Coverage Report

Created: 2025-12-31 06:58

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