Coverage Report

Created: 2025-12-04 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/ssl/quic/quic_tls.c
Line
Count
Source
1
/*
2
 * Copyright 2022-2025 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_record_util.h"
14
#include "internal/quic_error.h"
15
#include "internal/quic_types.h"
16
#include "internal/ssl_unwrap.h"
17
18
#define QUIC_TLS_FATAL(rl, ad, err) \
19
35
    do { \
20
35
        if ((rl) != NULL) (rl)->alert = (ad); \
21
35
        ERR_raise(ERR_LIB_SSL, (err)); \
22
35
        if ((rl) != NULL) (rl)->qtls->inerror = 1; \
23
35
    } while(0)
24
25
struct quic_tls_st {
26
    QUIC_TLS_ARGS args;
27
28
    /*
29
     * Transport parameters which client should send. Buffer lifetime must
30
     * exceed the lifetime of the QUIC_TLS object.
31
     */
32
    const unsigned char *local_transport_params;
33
    size_t local_transport_params_len;
34
35
    ERR_STATE *error_state;
36
37
    /*
38
     * QUIC error code (usually in the TLS Alert-mapped CRYPTO_ERR range). Valid
39
     * only if inerror is 1.
40
     */
41
    uint64_t error_code;
42
43
    /*
44
     * Error message with static storage duration. Valid only if inerror is 1.
45
     * Should be suitable for encapsulation in a CONNECTION_CLOSE frame.
46
     */
47
    const char *error_msg;
48
49
    /* Whether our SSL object for TLS has been configured for use in QUIC */
50
    unsigned int configured : 1;
51
52
    /* Set if we have hit any error state */
53
    unsigned int inerror : 1;
54
55
    /* Set if the handshake has completed */
56
    unsigned int complete : 1;
57
58
    /* Set if we have consumed the local transport parameters yet. */
59
    unsigned int local_transport_params_consumed : 1;
60
};
61
62
struct ossl_record_layer_st {
63
    QUIC_TLS *qtls;
64
65
    /* Protection level */
66
    int level;
67
68
    /* Only used for retry flags */
69
    BIO *dummybio;
70
71
    /* Number of bytes written so far if we are part way through a write */
72
    size_t written;
73
74
    /* If we are part way through a write, a copy of the template */
75
    OSSL_RECORD_TEMPLATE template;
76
77
    /*
78
     * If we hit an error, what alert code should be used
79
     */
80
    int alert;
81
82
    /* Amount of crypto stream data we read in the last call to quic_read_record */
83
    size_t recread;
84
85
    /* Amount of crypto stream data read but not yet released */
86
    size_t recunreleased;
87
88
    /* Callbacks */
89
    OSSL_FUNC_rlayer_msg_callback_fn *msg_callback;
90
    void *cbarg;
91
};
92
93
static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio);
94
static int quic_free(OSSL_RECORD_LAYER *r);
95
96
static int
97
quic_new_record_layer(OSSL_LIB_CTX *libctx, const char *propq, int vers,
98
                      int role, int direction, int level, uint16_t epoch,
99
                      unsigned char *secret, size_t secretlen,
100
                      unsigned char *key, size_t keylen, unsigned char *iv,
101
                      size_t ivlen, unsigned char *mackey, size_t mackeylen,
102
                      const EVP_CIPHER *ciph, size_t taglen,
103
                      int mactype,
104
                      const EVP_MD *md, COMP_METHOD *comp,
105
                      const EVP_MD *kdfdigest, BIO *prev, BIO *transport,
106
                      BIO *next, BIO_ADDR *local, BIO_ADDR *peer,
107
                      const OSSL_PARAM *settings, const OSSL_PARAM *options,
108
                      const OSSL_DISPATCH *fns, void *cbarg, void *rlarg,
109
                      OSSL_RECORD_LAYER **retrl)
110
133k
{
111
133k
    OSSL_RECORD_LAYER *rl = OPENSSL_zalloc(sizeof(*rl));
112
133k
    int qdir;
113
133k
    uint32_t suite_id = 0;
114
115
133k
    if (rl == NULL) {
116
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
117
0
        return 0;
118
0
    }
119
120
133k
    rl->qtls = (QUIC_TLS *)rlarg;
121
133k
    rl->level = level;
122
133k
    if (!quic_set1_bio(rl, transport)) {
123
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
124
0
        goto err;
125
0
    }
126
133k
    rl->cbarg = cbarg;
127
133k
    *retrl = rl;
128
129
133k
    if (fns != NULL) {
130
399k
        for (; fns->function_id != 0; fns++) {
131
266k
            switch (fns->function_id) {
132
0
                break;
133
0
            case OSSL_FUNC_RLAYER_MSG_CALLBACK:
134
0
                rl->msg_callback = OSSL_FUNC_rlayer_msg_callback(fns);
135
0
                break;
136
266k
            default:
137
                /* Just ignore anything we don't understand */
138
266k
                break;
139
266k
            }
140
266k
        }
141
133k
    }
142
143
133k
    if (level == OSSL_RECORD_PROTECTION_LEVEL_NONE)
144
97.8k
        return 1;
145
146
35.1k
    if (direction == OSSL_RECORD_DIRECTION_READ)
147
17.5k
        qdir = 0;
148
17.5k
    else
149
17.5k
        qdir = 1;
150
151
35.1k
    if (rl->qtls->args.ossl_quic) {
152
35.1k
#ifndef OPENSSL_NO_QUIC
153
        /*
154
         * We only look up the suite_id/MD for internal callers. Not used in the
155
         * public API. We assume that a 3rd party QUIC stack will want to
156
         * figure this out by itself (e.g. so that they could add new
157
         * ciphersuites at a different pace to us)
158
         */
159
35.1k
        if (EVP_CIPHER_is_a(ciph, "AES-128-GCM")) {
160
738
            suite_id = QRL_SUITE_AES128GCM;
161
34.4k
        } else if (EVP_CIPHER_is_a(ciph, "AES-256-GCM")) {
162
32.3k
            suite_id = QRL_SUITE_AES256GCM;
163
32.3k
        } else if (EVP_CIPHER_is_a(ciph, "CHACHA20-POLY1305")) {
164
2.06k
            suite_id = QRL_SUITE_CHACHA20POLY1305;
165
2.06k
        } else {
166
0
            QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE);
167
0
            goto err;
168
0
        }
169
170
        /* We pass a ref to the md in a successful yield_secret_cb call */
171
        /* TODO(QUIC FUTURE): This cast is horrible. We should try and remove it */
172
35.1k
        if (!EVP_MD_up_ref((EVP_MD *)kdfdigest)) {
173
0
            QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
174
0
            goto err;
175
0
        }
176
#else
177
        if (!ossl_assert("Should not happen" == NULL))
178
            goto err;
179
#endif
180
35.1k
    } else {
181
0
        kdfdigest = NULL;
182
0
    }
183
184
35.1k
    if (!rl->qtls->args.yield_secret_cb(level, qdir, suite_id,
185
35.1k
                                        (EVP_MD *)kdfdigest, secret, secretlen,
186
35.1k
                                        rl->qtls->args.yield_secret_cb_arg)) {
187
19
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
188
19
        EVP_MD_free((EVP_MD *)kdfdigest);
189
19
        goto err;
190
19
    }
191
192
35.1k
    return 1;
193
19
 err:
194
19
    *retrl = NULL;
195
19
    quic_free(rl);
196
19
    return 0;
197
35.1k
}
198
199
static int quic_free(OSSL_RECORD_LAYER *rl)
200
299k
{
201
299k
    if (rl == NULL)
202
0
        return 1;
203
204
299k
    BIO_free(rl->dummybio);
205
299k
    OPENSSL_free(rl);
206
299k
    return 1;
207
299k
}
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
118k
{
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
118k
    return 0;
234
118k
}
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
68.3k
{
240
68.3k
    return 1;
241
68.3k
}
242
243
static int quic_write_records(OSSL_RECORD_LAYER *rl,
244
                              OSSL_RECORD_TEMPLATE *template,
245
                              size_t numtempl)
246
80.1k
{
247
80.1k
    size_t consumed;
248
80.1k
    unsigned char alert;
249
250
80.1k
    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
80.1k
    BIO_clear_retry_flags(rl->dummybio);
257
258
80.1k
    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
80.1k
    switch (template->type) {
289
11.7k
    case SSL3_RT_ALERT:
290
11.7k
        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.7k
        alert = template->buf[1];
304
305
11.7k
        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.7k
        break;
310
311
68.3k
    case SSL3_RT_HANDSHAKE:
312
        /*
313
         * We expect this to only fail on some fatal error (e.g. malloc
314
         * failure)
315
         */
316
68.3k
        if (!rl->qtls->args.crypto_send_cb(template->buf + rl->written,
317
68.3k
                                           template->buflen - rl->written,
318
68.3k
                                           &consumed,
319
68.3k
                                           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
68.3k
        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
68.3k
        rl->written = 0;
346
68.3k
        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
80.1k
    }
353
354
80.1k
    return OSSL_RECORD_RETURN_SUCCESS;
355
80.1k
}
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
74.1M
{
367
74.1M
    if (rl->recread != 0 || rl->recunreleased != 0)
368
0
        return OSSL_RECORD_RETURN_FATAL;
369
370
74.1M
    BIO_clear_retry_flags(rl->dummybio);
371
372
74.1M
    if (!rl->qtls->args.crypto_recv_rcd_cb(data, datalen,
373
74.1M
                                           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
74.1M
    if (*datalen == 0) {
379
74.0M
        BIO_set_retry_read(rl->dummybio);
380
74.0M
        return OSSL_RECORD_RETURN_RETRY;
381
74.0M
    }
382
383
77.8k
    *rechandle = rl;
384
77.8k
    *rversion = TLS1_3_VERSION;
385
77.8k
    *type = SSL3_RT_HANDSHAKE;
386
77.8k
    rl->recread = rl->recunreleased = *datalen;
387
    /* epoch/seq_num are not relevant for TLS */
388
389
77.8k
    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
77.8k
    return OSSL_RECORD_RETURN_SUCCESS;
415
74.1M
}
416
417
static int quic_release_record(OSSL_RECORD_LAYER *rl, void *rechandle,
418
                               size_t length)
419
117k
{
420
117k
    if (!ossl_assert(rl->recread > 0)
421
117k
            || !ossl_assert(rl->recunreleased <= rl->recread)
422
117k
            || !ossl_assert(rl == rechandle)
423
117k
            || !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
117k
    if (rl->recunreleased == length) {
429
34.5k
        if (!rl->qtls->args.crypto_release_rcd_cb(rl->recread,
430
34.5k
                                                  rl->qtls->args.crypto_release_rcd_cb_arg)) {
431
0
            QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
432
0
            return OSSL_RECORD_RETURN_FATAL;
433
0
        }
434
34.5k
        rl->recread = 0;
435
34.5k
    }
436
117k
    rl->recunreleased -= length;
437
117k
    return OSSL_RECORD_RETURN_SUCCESS;
438
117k
}
439
440
static int quic_get_alert_code(OSSL_RECORD_LAYER *rl)
441
16
{
442
16
    return rl->alert;
443
16
}
444
445
static int quic_set_protocol_version(OSSL_RECORD_LAYER *rl, int version)
446
108k
{
447
    /* We only support TLSv1.3, so its bad if we negotiate anything else */
448
108k
    if (!ossl_assert(version == TLS1_3_VERSION)) {
449
0
        QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
450
0
        return 0;
451
0
    }
452
453
108k
    return 1;
454
108k
}
455
456
static void quic_set_plain_alerts(OSSL_RECORD_LAYER *rl, int allow)
457
39.9k
{
458
    /* We don't care */
459
39.9k
}
460
461
static void quic_set_first_handshake(OSSL_RECORD_LAYER *rl, int first)
462
280k
{
463
    /* We don't care */
464
280k
}
465
466
static void quic_set_max_pipelines(OSSL_RECORD_LAYER *rl, size_t max_pipelines)
467
0
{
468
    /* We don't care */
469
0
}
470
471
static void quic_get_state(OSSL_RECORD_LAYER *rl, const char **shortstr,
472
                    const char **longstr)
473
0
{
474
    /*
475
     * According to the docs, valid read state strings are: "RH"/"read header",
476
     * "RB"/"read body", and "unknown"/"unknown". We don't read records in quite
477
     * that way, so we report every "normal" state as "read header". In the
478
     * event of error then we report "unknown".
479
     */
480
481
0
    if (rl->qtls->inerror) {
482
0
        if (shortstr != NULL)
483
0
            *shortstr = "unknown";
484
0
        if (longstr != NULL)
485
0
            *longstr = "unknown";
486
0
    } else {
487
0
        if (shortstr != NULL)
488
0
            *shortstr = "RH";
489
0
        if (longstr != NULL)
490
0
            *longstr = "read header";
491
0
    }
492
0
}
493
494
static int quic_set_options(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options)
495
0
{
496
    /*
497
     * We don't support any options yet - but we might do at some point so
498
     * this could be useful.
499
     */
500
0
    return 1;
501
0
}
502
503
static const COMP_METHOD *quic_get_compression(OSSL_RECORD_LAYER *rl)
504
0
{
505
    /* We only support TLSv1.3 which doesn't have compression */
506
0
    return NULL;
507
0
}
508
509
static void quic_set_max_frag_len(OSSL_RECORD_LAYER *rl, size_t max_frag_len)
510
0
{
511
    /* This really doesn't make any sense for QUIC. Ignore it */
512
0
}
513
514
static int quic_alloc_buffers(OSSL_RECORD_LAYER *rl)
515
0
{
516
    /*
517
     * This is a hint only. We don't support it (yet), so just ignore the
518
     * request
519
     */
520
0
    return 1;
521
0
}
522
523
static int quic_free_buffers(OSSL_RECORD_LAYER *rl)
524
0
{
525
    /*
526
     * This is a hint only. We don't support it (yet), so just ignore the
527
     * request
528
     */
529
0
    return 1;
530
0
}
531
532
static int quic_set1_bio(OSSL_RECORD_LAYER *rl, BIO *bio)
533
423k
{
534
423k
    if (bio != NULL && !BIO_up_ref(bio))
535
0
        return 0;
536
423k
    BIO_free(rl->dummybio);
537
423k
    rl->dummybio = bio;
538
539
423k
    return 1;
540
423k
}
541
542
/*
543
 * Never called functions
544
 *
545
 * Due to the way we are configured and used we never expect any of the next set
546
 * of functions to be called. Therefore we set them to always fail.
547
 */
548
549
static size_t quic_app_data_pending(OSSL_RECORD_LAYER *rl)
550
0
{
551
0
    QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
552
0
    return (size_t)ossl_assert(0);
553
0
}
554
555
static size_t quic_get_max_record_overhead(OSSL_RECORD_LAYER *rl)
556
0
{
557
0
    QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
558
0
    return (size_t)ossl_assert(0);
559
0
}
560
561
static int quic_increment_sequence_ctr(OSSL_RECORD_LAYER *rl)
562
0
{
563
0
    QUIC_TLS_FATAL(rl, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
564
0
    return ossl_assert(0);
565
0
}
566
567
/* End of never called functions */
568
569
static const OSSL_RECORD_METHOD quic_tls_record_method = {
570
    quic_new_record_layer,
571
    quic_free,
572
    quic_unprocessed_read_pending,
573
    quic_processed_read_pending,
574
    quic_app_data_pending, /* Never called */
575
    quic_get_max_records,
576
    quic_write_records,
577
    quic_retry_write_records,
578
    quic_read_record,
579
    quic_release_record,
580
    quic_get_alert_code,
581
    quic_set1_bio,
582
    quic_set_protocol_version,
583
    quic_set_plain_alerts,
584
    quic_set_first_handshake,
585
    quic_set_max_pipelines,
586
    NULL, /* set_in_init: Optional - we don't need it */
587
    quic_get_state,
588
    quic_set_options,
589
    quic_get_compression,
590
    quic_set_max_frag_len,
591
    quic_get_max_record_overhead, /* Never called */
592
    quic_increment_sequence_ctr, /* Never called */
593
    quic_alloc_buffers,
594
    quic_free_buffers
595
};
596
597
static int add_transport_params_cb(SSL *s, unsigned int ext_type,
598
                                   unsigned int context,
599
                                   const unsigned char **out, size_t *outlen,
600
                                   X509 *x, size_t chainidx, int *al,
601
                                   void *add_arg)
602
54.9k
{
603
54.9k
    QUIC_TLS *qtls = add_arg;
604
605
54.9k
    *out = qtls->local_transport_params;
606
54.9k
    *outlen = qtls->local_transport_params_len;
607
54.9k
    qtls->local_transport_params_consumed = 1;
608
54.9k
    return 1;
609
54.9k
}
610
611
static void free_transport_params_cb(SSL *s, unsigned int ext_type,
612
                                     unsigned int context,
613
                                     const unsigned char *out,
614
                                     void *add_arg)
615
54.9k
{
616
54.9k
}
617
618
static int parse_transport_params_cb(SSL *s, unsigned int ext_type,
619
                                     unsigned int context,
620
                                     const unsigned char *in,
621
                                     size_t inlen, X509 *x,
622
                                     size_t chainidx,
623
                                     int *al, void *parse_arg)
624
21.4k
{
625
21.4k
    QUIC_TLS *qtls = parse_arg;
626
627
21.4k
    return qtls->args.got_transport_params_cb(in, inlen,
628
21.4k
                                              qtls->args.got_transport_params_cb_arg);
629
21.4k
}
630
631
QUIC_TLS *ossl_quic_tls_new(const QUIC_TLS_ARGS *args)
632
24.4k
{
633
24.4k
    QUIC_TLS *qtls;
634
635
24.4k
    if (args->crypto_send_cb == NULL
636
24.4k
        || args->crypto_recv_rcd_cb == NULL
637
24.4k
        || args->crypto_release_rcd_cb == NULL) {
638
0
        ERR_raise(ERR_LIB_SSL, ERR_R_PASSED_NULL_PARAMETER);
639
0
        return NULL;
640
0
    }
641
642
24.4k
    qtls = OPENSSL_zalloc(sizeof(*qtls));
643
24.4k
    if (qtls == NULL)
644
0
        return NULL;
645
646
24.4k
    if (args->ossl_quic && (qtls->error_state = OSSL_ERR_STATE_new()) == NULL) {
647
0
        OPENSSL_free(qtls);
648
0
        return NULL;
649
0
    }
650
651
24.4k
    qtls->args = *args;
652
24.4k
    return qtls;
653
24.4k
}
654
655
void ossl_quic_tls_free(QUIC_TLS *qtls)
656
130k
{
657
130k
    if (qtls == NULL)
658
75.9k
        return;
659
54.8k
    OSSL_ERR_STATE_free(qtls->error_state);
660
54.8k
    OPENSSL_free(qtls);
661
54.8k
}
662
663
static int raise_error(QUIC_TLS *qtls, uint64_t error_code,
664
                       const char *error_msg,
665
                       const char *src_file,
666
                       int src_line,
667
                       const char *src_func)
668
5.43k
{
669
    /*
670
     * When QTLS fails, add a "cover letter" error with information, potentially
671
     * with any underlying libssl errors underneath it (but our cover error may
672
     * be the only error in some cases). Then capture this into an ERR_STATE so
673
     * we can report it later if need be when the QUIC_CHANNEL asks for it.
674
     * For external QUIC TLS we just raise the error.
675
     */
676
5.43k
    ERR_new();
677
5.43k
    ERR_set_debug(src_file, src_line, src_func);
678
5.43k
    ERR_set_error(ERR_LIB_SSL, SSL_R_QUIC_HANDSHAKE_LAYER_ERROR,
679
5.43k
                  "handshake layer error, error code %llu (0x%llx) (\"%s\")",
680
5.43k
                  error_code, error_code, error_msg);
681
682
5.43k
    if (qtls->args.ossl_quic) {
683
5.43k
        OSSL_ERR_STATE_save_to_mark(qtls->error_state);
684
685
        /*
686
         * We record the error information reported via the QUIC protocol
687
         * separately.
688
         */
689
5.43k
        qtls->error_code        = error_code;
690
5.43k
        qtls->error_msg         = error_msg;
691
5.43k
        qtls->inerror           = 1;
692
693
5.43k
        ERR_pop_to_mark();
694
5.43k
    }
695
5.43k
    return 0;
696
5.43k
}
697
698
#define RAISE_ERROR(qtls, error_code, error_msg) \
699
5.43k
    raise_error((qtls), (error_code), (error_msg), \
700
5.43k
                OPENSSL_FILE, OPENSSL_LINE, OPENSSL_FUNC)
701
702
#ifndef OPENSSL_NO_QUIC
703
# define RAISE_INTERNAL_ERROR(qtls) \
704
5.40k
    RAISE_ERROR((qtls), OSSL_QUIC_ERR_INTERNAL_ERROR, "internal error")
705
#else
706
# define RAISE_INTERNAL_ERROR(qtls) \
707
    RAISE_ERROR((qtls), 0x01, "internal error")
708
#endif
709
710
int ossl_quic_tls_configure(QUIC_TLS *qtls)
711
24.4k
{
712
24.4k
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
713
24.4k
    BIO *nullbio;
714
715
24.4k
    if (sc == NULL || !SSL_set_min_proto_version(qtls->args.s, TLS1_3_VERSION))
716
0
        return RAISE_INTERNAL_ERROR(qtls);
717
718
24.4k
    nullbio = BIO_new(BIO_s_null());
719
24.4k
    if (nullbio == NULL)
720
0
        return RAISE_INTERNAL_ERROR(qtls);
721
722
    /*
723
     * Our custom record layer doesn't use the BIO - but libssl generally
724
     * expects one to be present.
725
     */
726
24.4k
    SSL_set_bio(qtls->args.s, nullbio, nullbio);
727
728
24.4k
    SSL_clear_options(qtls->args.s, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
729
24.4k
    ossl_ssl_set_custom_record_layer(sc, &quic_tls_record_method, qtls);
730
731
24.4k
    if (!ossl_tls_add_custom_ext_intern(NULL, &sc->cert->custext,
732
24.4k
                                        qtls->args.is_server ? ENDPOINT_SERVER
733
24.4k
                                                             : ENDPOINT_CLIENT,
734
24.4k
                                        TLSEXT_TYPE_quic_transport_parameters,
735
24.4k
                                        SSL_EXT_TLS1_3_ONLY
736
24.4k
                                        | SSL_EXT_CLIENT_HELLO
737
24.4k
                                        | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
738
24.4k
                                        add_transport_params_cb,
739
24.4k
                                        free_transport_params_cb, qtls,
740
24.4k
                                        parse_transport_params_cb, qtls))
741
0
        return 0;
742
743
24.4k
    sc->s3.flags |= TLS1_FLAGS_QUIC;
744
745
24.4k
    return 1;
746
24.4k
}
747
748
#ifndef OPENSSL_NO_QUIC
749
int ossl_quic_tls_tick(QUIC_TLS *qtls)
750
25.9M
{
751
25.9M
    int ret, err;
752
25.9M
    const unsigned char *alpn;
753
25.9M
    unsigned int alpnlen;
754
755
25.9M
    if (qtls->inerror)
756
3.19k
        return 0;
757
758
    /*
759
     * SSL_get_error does not truly know what the cause of an SSL_read failure
760
     * is and to some extent guesses based on contextual information. In
761
     * particular, if there is _any_ ERR on the error stack, SSL_ERROR_SSL or
762
     * SSL_ERROR_SYSCALL will be returned no matter what and there is no
763
     * possibility of SSL_ERROR_WANT_READ/WRITE being returned, even if that was
764
     * the actual cause of the SSL_read() failure.
765
     *
766
     * This means that ordinarily, the below code might not work right if the
767
     * application has any ERR on the error stack. In order to make this code
768
     * perform correctly regardless of prior ERR state, we use a variant of
769
     * SSL_get_error() which ignores the error stack. However, some ERRs are
770
     * raised by SSL_read() and actually indicate that something has gone wrong
771
     * during the call to SSL_read(). We therefore adopt a strategy of marking
772
     * the ERR stack and seeing if any errors get appended during the call to
773
     * SSL_read(). If they are, we assume SSL_read() has raised an error and
774
     * that we should use normal SSL_get_error() handling.
775
     *
776
     * NOTE: Ensure all escape paths from this function call
777
     * ERR_clear_to_mark(). The RAISE macros handle this in failure cases.
778
     */
779
25.9M
    ERR_set_mark();
780
781
25.9M
    if (!qtls->configured) {
782
24.4k
        SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
783
24.4k
        SSL_CTX *sctx;
784
785
24.4k
        if (sc == NULL)
786
0
            return RAISE_INTERNAL_ERROR(qtls);
787
24.4k
        sctx = SSL_CONNECTION_GET_CTX(sc);
788
789
        /*
790
         * No matter how the user has configured us, there are certain
791
         * requirements for QUIC-TLS that we enforce
792
         */
793
794
        /* ALPN is a requirement for QUIC and must be set */
795
24.4k
        if (qtls->args.is_server) {
796
0
            if (sctx->ext.alpn_select_cb == NULL)
797
0
                return RAISE_INTERNAL_ERROR(qtls);
798
24.4k
        } else {
799
24.4k
            if (sc->ext.alpn == NULL || sc->ext.alpn_len == 0)
800
0
                return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
801
24.4k
                                   "ALPN must be configured when using QUIC");
802
24.4k
        }
803
804
24.4k
        if (!ossl_quic_tls_configure(qtls))
805
0
            return RAISE_INTERNAL_ERROR(qtls);
806
807
24.4k
        sc->s3.flags |= TLS1_FLAGS_QUIC_INTERNAL;
808
809
24.4k
        if (qtls->args.is_server)
810
0
            SSL_set_accept_state(qtls->args.s);
811
24.4k
        else
812
24.4k
            SSL_set_connect_state(qtls->args.s);
813
814
24.4k
        qtls->configured = 1;
815
24.4k
    }
816
817
25.9M
    if (qtls->complete)
818
        /*
819
         * There should never be app data to read, but calling SSL_read() will
820
         * ensure any post-handshake messages are processed.
821
         */
822
9.13M
        ret = SSL_read(qtls->args.s, NULL, 0);
823
16.7M
    else
824
16.7M
        ret = SSL_do_handshake(qtls->args.s);
825
826
25.9M
    if (ret <= 0) {
827
25.9M
        err = ossl_ssl_get_error(qtls->args.s, ret,
828
25.9M
                                 /*check_err=*/ERR_count_to_mark() > 0);
829
830
25.9M
        switch (err) {
831
25.9M
        case SSL_ERROR_WANT_READ:
832
25.9M
        case SSL_ERROR_WANT_WRITE:
833
25.9M
        case SSL_ERROR_WANT_CLIENT_HELLO_CB:
834
25.9M
        case SSL_ERROR_WANT_X509_LOOKUP:
835
25.9M
        case SSL_ERROR_WANT_RETRY_VERIFY:
836
25.9M
            ERR_pop_to_mark();
837
25.9M
            return 1;
838
839
5.40k
        default:
840
5.40k
            return RAISE_INTERNAL_ERROR(qtls);
841
25.9M
        }
842
25.9M
    }
843
844
5.81k
    if (!qtls->complete) {
845
        /* Validate that we have ALPN */
846
5.81k
        SSL_get0_alpn_selected(qtls->args.s, &alpn, &alpnlen);
847
5.81k
        if (alpn == NULL || alpnlen == 0)
848
24
            return RAISE_ERROR(qtls, OSSL_QUIC_ERR_CRYPTO_NO_APP_PROTO,
849
5.81k
                               "no application protocol negotiated");
850
851
5.78k
        qtls->complete = 1;
852
5.78k
        ERR_pop_to_mark();
853
5.78k
        return qtls->args.handshake_complete_cb(qtls->args.handshake_complete_cb_arg);
854
5.81k
    }
855
856
0
    ERR_pop_to_mark();
857
0
    return 1;
858
5.81k
}
859
#endif
860
861
void ossl_quic_tls_clear(QUIC_TLS *qtls)
862
151k
{
863
151k
    if (qtls == NULL)
864
151k
        return;
865
0
    qtls->local_transport_params_consumed = 0;
866
0
}
867
868
int ossl_quic_tls_set_transport_params(QUIC_TLS *qtls,
869
                                       const unsigned char *transport_params,
870
                                       size_t transport_params_len)
871
24.4k
{
872
24.4k
    if (qtls->local_transport_params_consumed)
873
0
        return 0;
874
875
24.4k
    qtls->local_transport_params       = transport_params;
876
24.4k
    qtls->local_transport_params_len   = transport_params_len;
877
24.4k
    return 1;
878
24.4k
}
879
880
int ossl_quic_tls_get_error(QUIC_TLS *qtls,
881
                            uint64_t *error_code,
882
                            const char **error_msg,
883
                            ERR_STATE **error_state)
884
74.0M
{
885
74.0M
    if (qtls->inerror) {
886
18.7k
        *error_code     = qtls->error_code;
887
18.7k
        *error_msg      = qtls->error_msg;
888
18.7k
        *error_state    = qtls->error_state;
889
18.7k
    }
890
891
74.0M
    return qtls->inerror;
892
74.0M
}
893
894
/*
895
 * Returns true if the last handshake record message we processed was a
896
 * CertificateRequest
897
 */
898
int ossl_quic_tls_is_cert_request(QUIC_TLS *qtls)
899
116
{
900
116
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
901
902
116
    if (sc == NULL)
903
0
        return 0;
904
905
116
    return sc->s3.tmp.message_type == SSL3_MT_CERTIFICATE_REQUEST;
906
116
}
907
908
/*
909
 * Returns true if the last session associated with the connection has an
910
 * invalid max_early_data value for QUIC.
911
 */
912
int ossl_quic_tls_has_bad_max_early_data(QUIC_TLS *qtls)
913
24
{
914
24
    uint32_t max_early_data = SSL_get0_session(qtls->args.s)->ext.max_early_data;
915
916
    /*
917
     * If max_early_data was present we always ensure a non-zero value is
918
     * stored in the session for QUIC. Therefore if max_early_data == 0 here
919
     * we can be confident that it was not present in the NewSessionTicket
920
     */
921
24
    return max_early_data != 0xffffffff && max_early_data != 0;
922
24
}
923
924
int ossl_quic_tls_set_early_data_enabled(QUIC_TLS *qtls, int enabled)
925
0
{
926
0
    SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(qtls->args.s);
927
928
0
    if (sc == NULL || !SSL_IS_QUIC_HANDSHAKE(sc) || !SSL_in_before(qtls->args.s))
929
0
        return 0;
930
931
0
    if (!enabled) {
932
0
        sc->max_early_data = 0;
933
0
        sc->early_data_state = SSL_EARLY_DATA_NONE;
934
0
        return 1;
935
0
    }
936
937
0
    if (sc->server) {
938
0
        sc->max_early_data = 0xffffffff;
939
0
        sc->early_data_state = SSL_EARLY_DATA_ACCEPTING;
940
0
        return 1;
941
0
    }
942
943
0
    if ((sc->session == NULL || sc->session->ext.max_early_data != 0xffffffff)
944
0
        && sc->psk_use_session_cb == NULL)
945
0
        return 0;
946
947
0
    sc->early_data_state = SSL_EARLY_DATA_CONNECTING;
948
0
    return 1;
949
0
}