Coverage Report

Created: 2026-08-31 06:56

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