Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/ssl/tls13_enc.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2026 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
10
#include <stdlib.h>
11
#include "ssl_local.h"
12
#include "internal/ktls.h"
13
#include "record/record_local.h"
14
#include "internal/cryptlib.h"
15
#include "internal/ssl_unwrap.h"
16
#include <openssl/evp.h>
17
#include <openssl/kdf.h>
18
#include <openssl/core_names.h>
19
20
198k
#define TLS13_MAX_LABEL_LEN 249
21
22
/* ASCII: "dtls13", in hex for EBCDIC compatibility */
23
static const unsigned char label_prefix_dtls13[] = "\x64\x74\x6C\x73\x31\x33";
24
/* ASCII: "tls13 ", in hex for EBCDIC compatibility */
25
static const unsigned char label_prefix_tls13[] = "\x74\x6C\x73\x31\x33\x20";
26
27
/*
28
 * Given a |secret|; a |label_prefix| of length |label_prefix_len|; a |label|
29
 * of length |labellen|; and |data| of length |datalen| (e.g. typically a hash
30
 * of the handshake messages), derive a new secret |outlen| bytes long and
31
 * store it in the location pointed to be |out|.
32
 * The |data| value may be zero length. Any errors will be treated as fatal if
33
 * |fatal| is set. Returns 1 on success  0 on failure.
34
 * If |raise_error| is set, ERR_raise is called on failure.
35
 */
36
static int hkdf_expand(OSSL_LIB_CTX *libctx, const char *propq,
37
    const EVP_MD *md,
38
    const unsigned char *secret,
39
    const unsigned char *label_prefix, size_t label_prefix_len,
40
    const unsigned char *label, size_t labellen,
41
    const unsigned char *data, size_t datalen,
42
    unsigned char *out, size_t outlen, int raise_error)
43
198k
{
44
198k
    EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_TLS1_3_KDF, propq);
45
198k
    EVP_KDF_CTX *kctx;
46
198k
    OSSL_PARAM params[8], *p = params;
47
198k
    int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
48
198k
    const char *mdname = EVP_MD_get0_name(md);
49
198k
    int ret;
50
198k
    size_t hashlen;
51
52
198k
    kctx = EVP_KDF_CTX_new(kdf);
53
198k
    EVP_KDF_free(kdf);
54
198k
    if (kctx == NULL)
55
0
        return 0;
56
57
198k
    if (labellen > TLS13_MAX_LABEL_LEN) {
58
0
        if (raise_error)
59
            /*
60
             * Probably we have been called from SSL_export_keying_material(),
61
             * or SSL_export_keying_material_early().
62
             */
63
0
            ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
64
65
0
        EVP_KDF_CTX_free(kctx);
66
0
        return 0;
67
0
    }
68
69
198k
    if ((ret = EVP_MD_get_size(md)) <= 0) {
70
0
        EVP_KDF_CTX_free(kctx);
71
0
        if (raise_error)
72
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
73
0
        return 0;
74
0
    }
75
198k
    hashlen = (size_t)ret;
76
77
198k
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
78
198k
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
79
198k
        (char *)mdname, 0);
80
198k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
81
198k
        (unsigned char *)secret, hashlen);
82
198k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
83
198k
        (unsigned char *)label_prefix,
84
198k
        label_prefix_len);
85
198k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
86
198k
        (unsigned char *)label, labellen);
87
198k
    if (data != NULL)
88
20.3k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_DATA,
89
20.3k
            (unsigned char *)data,
90
20.3k
            datalen);
91
198k
    if (propq != NULL)
92
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
93
0
            (char *)propq, 0);
94
95
198k
    *p++ = OSSL_PARAM_construct_end();
96
97
198k
    ret = EVP_KDF_derive(kctx, out, outlen, params) <= 0;
98
198k
    EVP_KDF_CTX_free(kctx);
99
100
198k
    if (ret != 0) {
101
0
        if (raise_error)
102
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
103
0
    }
104
105
198k
    return ret == 0;
106
198k
}
107
108
int tls13_hkdf_expand_ex(OSSL_LIB_CTX *libctx, const char *propq,
109
    const EVP_MD *md,
110
    const unsigned char *secret,
111
    const unsigned char *label, size_t labellen,
112
    const unsigned char *data, size_t datalen,
113
    unsigned char *out, size_t outlen, int raise_error)
114
138k
{
115
    /* This function only supports TLSv1.3 and not DTLSv1.3 */
116
138k
    return hkdf_expand(libctx, propq, md, secret, label_prefix_tls13,
117
138k
        sizeof(label_prefix_tls13) - 1,
118
138k
        label, labellen, data, datalen, out, outlen,
119
138k
        raise_error);
120
138k
}
121
122
int tls13_hkdf_expand(SSL_CONNECTION *s, const EVP_MD *md,
123
    const unsigned char *secret,
124
    const unsigned char *label, size_t labellen,
125
    const unsigned char *data, size_t datalen,
126
    unsigned char *out, size_t outlen, int fatal)
127
59.9k
{
128
59.9k
    int ret;
129
59.9k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
130
59.9k
    const int isdtls = SSL_CONNECTION_IS_DTLS(s);
131
59.9k
    const unsigned char *label_prefix = isdtls ? label_prefix_dtls13
132
59.9k
                                               : label_prefix_tls13;
133
59.9k
    const size_t label_prefix_len = isdtls ? sizeof(label_prefix_dtls13) - 1
134
59.9k
                                           : sizeof(label_prefix_tls13) - 1;
135
136
59.9k
    ret = hkdf_expand(sctx->libctx, sctx->propq, md, secret, label_prefix,
137
59.9k
        label_prefix_len, label, labellen, data,
138
59.9k
        datalen, out, outlen, !fatal);
139
59.9k
    if (ret == 0 && fatal)
140
59.9k
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
141
142
59.9k
    return ret;
143
59.9k
}
144
145
/*
146
 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
147
 * success  0 on failure.
148
 */
149
int tls13_derive_key(SSL_CONNECTION *s, const EVP_MD *md,
150
    const unsigned char *secret,
151
    unsigned char *key, size_t keylen)
152
68.7k
{
153
    /* ASCII: "key", in hex for EBCDIC compatibility */
154
68.7k
    static const unsigned char keylabel[] = "\x6B\x65\x79";
155
156
68.7k
    return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
157
68.7k
        NULL, 0, key, keylen, 1);
158
68.7k
}
159
160
/*
161
 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
162
 * success  0 on failure.
163
 */
164
int tls13_derive_iv(SSL_CONNECTION *s, const EVP_MD *md,
165
    const unsigned char *secret,
166
    unsigned char *iv, size_t ivlen)
167
68.7k
{
168
    /* ASCII: "iv", in hex for EBCDIC compatibility */
169
68.7k
    static const unsigned char ivlabel[] = "\x69\x76";
170
171
68.7k
    return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
172
68.7k
        NULL, 0, iv, ivlen, 1);
173
68.7k
}
174
175
int tls13_derive_finishedkey(SSL_CONNECTION *s, const EVP_MD *md,
176
    const unsigned char *secret,
177
    unsigned char *fin, size_t finlen)
178
46.4k
{
179
    /* ASCII: "finished", in hex for EBCDIC compatibility */
180
46.4k
    static const unsigned char finishedlabel[] = "\x66\x69\x6E\x69\x73\x68\x65\x64";
181
182
46.4k
    return tls13_hkdf_expand(s, md, secret, finishedlabel,
183
46.4k
        sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
184
46.4k
}
185
186
/*
187
 * Given a |secret| generate a |snkey| of length |snkeylen| bytes. Returns 1 on
188
 * success  0 on failure. (rfc9147 section 4.2.3)
189
 */
190
static int dtls13_derive_snkey(SSL_CONNECTION *s, const EVP_MD *md,
191
    const unsigned char *secret,
192
    unsigned char *snkey, size_t keylen)
193
0
{
194
    /* ASCII: "sn", in hex for EBCDIC compatibility */
195
0
    static const unsigned char sn_str[] = "\x73\x6E";
196
197
0
    return tls13_hkdf_expand(s, md, secret, sn_str, sizeof(sn_str) - 1,
198
0
        NULL, 0, snkey, keylen, 1);
199
0
}
200
201
/*
202
 * Given the previous secret |prevsecret| and a new input secret |insecret| of
203
 * length |insecretlen|, generate a new secret and store it in the location
204
 * pointed to by |outsecret|. Returns 1 on success  0 on failure.
205
 */
206
int tls13_generate_secret(SSL_CONNECTION *s, const EVP_MD *md,
207
    const unsigned char *prevsecret,
208
    const unsigned char *insecret,
209
    size_t insecretlen,
210
    unsigned char *outsecret)
211
12.7k
{
212
12.7k
    size_t mdlen;
213
12.7k
    int mdleni;
214
12.7k
    int ret;
215
12.7k
    EVP_KDF *kdf;
216
12.7k
    EVP_KDF_CTX *kctx;
217
12.7k
    OSSL_PARAM params[7], *p = params;
218
12.7k
    int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
219
12.7k
    const char *mdname = EVP_MD_get0_name(md);
220
    /* ASCII: "derived", in hex for EBCDIC compatibility */
221
12.7k
    static const char derived_secret_label[] = "\x64\x65\x72\x69\x76\x65\x64";
222
12.7k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
223
12.7k
    int isdtls = SSL_CONNECTION_IS_DTLS(s);
224
225
12.7k
    kdf = EVP_KDF_fetch(sctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF, sctx->propq);
226
12.7k
    kctx = EVP_KDF_CTX_new(kdf);
227
12.7k
    EVP_KDF_free(kdf);
228
12.7k
    if (kctx == NULL) {
229
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
230
0
        return 0;
231
0
    }
232
233
12.7k
    mdleni = EVP_MD_get_size(md);
234
    /* Ensure cast to size_t is safe */
235
12.7k
    if (!ossl_assert(mdleni > 0)) {
236
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
237
0
        EVP_KDF_CTX_free(kctx);
238
0
        return 0;
239
0
    }
240
12.7k
    mdlen = (size_t)mdleni;
241
242
12.7k
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
243
12.7k
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
244
12.7k
        (char *)mdname, 0);
245
12.7k
    if (insecret != NULL)
246
4.98k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
247
4.98k
            (unsigned char *)insecret,
248
4.98k
            insecretlen);
249
12.7k
    if (prevsecret != NULL)
250
7.74k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
251
7.74k
            (unsigned char *)prevsecret, mdlen);
252
12.7k
    if (isdtls)
253
0
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
254
0
            (unsigned char *)label_prefix_dtls13,
255
0
            sizeof(label_prefix_dtls13) - 1);
256
12.7k
    else
257
12.7k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
258
12.7k
            (unsigned char *)label_prefix_tls13,
259
12.7k
            sizeof(label_prefix_tls13) - 1);
260
261
12.7k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
262
12.7k
        (unsigned char *)derived_secret_label,
263
12.7k
        sizeof(derived_secret_label) - 1);
264
12.7k
    *p++ = OSSL_PARAM_construct_end();
265
266
12.7k
    ret = EVP_KDF_derive(kctx, outsecret, mdlen, params) <= 0;
267
268
12.7k
    if (ret != 0)
269
12.7k
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
270
271
12.7k
    EVP_KDF_CTX_free(kctx);
272
12.7k
    return ret == 0;
273
12.7k
}
274
275
/*
276
 * Given an input secret |insecret| of length |insecretlen| generate the
277
 * handshake secret. This requires the early secret to already have been
278
 * generated. Returns 1 on success  0 on failure.
279
 */
280
int tls13_generate_handshake_secret(SSL_CONNECTION *s,
281
    const unsigned char *insecret,
282
    size_t insecretlen)
283
23.4k
{
284
    /* Calls SSLfatal() if required */
285
23.4k
    return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
286
23.4k
        insecret, insecretlen,
287
23.4k
        (unsigned char *)&s->handshake_secret);
288
23.4k
}
289
290
/*
291
 * Given the handshake secret |prev| of length |prevlen| generate the master
292
 * secret and store its length in |*secret_size|. Returns 1 on success  0 on
293
 * failure.
294
 */
295
int tls13_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
296
    unsigned char *prev, size_t prevlen,
297
    size_t *secret_size)
298
12.3k
{
299
12.3k
    const EVP_MD *md = ssl_handshake_md(s);
300
12.3k
    int md_size;
301
302
12.3k
    md_size = EVP_MD_get_size(md);
303
12.3k
    if (md_size <= 0) {
304
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
305
0
        return 0;
306
0
    }
307
12.3k
    *secret_size = (size_t)md_size;
308
    /* Calls SSLfatal() if required */
309
12.3k
    return tls13_generate_secret(s, md, prev, NULL, 0, out);
310
12.3k
}
311
312
/*
313
 * Generates the mac for the Finished message. Returns the length of the MAC or
314
 * 0 on error.
315
 */
316
size_t tls13_final_finish_mac(SSL_CONNECTION *s, const char *str, size_t slen,
317
    unsigned char *out)
318
22.4k
{
319
22.4k
    const EVP_MD *md = ssl_handshake_md(s);
320
22.4k
    const char *mdname = EVP_MD_get0_name(md);
321
22.4k
    unsigned char hash[EVP_MAX_MD_SIZE];
322
22.4k
    unsigned char finsecret[EVP_MAX_MD_SIZE];
323
22.4k
    unsigned char *key = NULL;
324
22.4k
    size_t len = 0, hashlen;
325
22.4k
    OSSL_PARAM params[2], *p = params;
326
22.4k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
327
328
22.4k
    if (md == NULL) {
329
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
330
0
        return 0;
331
0
    }
332
333
    /* Safe to cast away const here since we're not "getting" any data */
334
22.4k
    if (sctx->propq != NULL)
335
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES,
336
0
            (char *)sctx->propq,
337
0
            0);
338
22.4k
    *p = OSSL_PARAM_construct_end();
339
340
22.4k
    if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
341
        /* SSLfatal() already called */
342
0
        goto err;
343
0
    }
344
345
22.4k
    if (str == SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->server_finished_label) {
346
12.4k
        key = s->server_finished_secret;
347
12.4k
    } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
348
10.0k
        key = s->client_finished_secret;
349
10.0k
    } else {
350
0
        if (!tls13_derive_finishedkey(s, md,
351
0
                s->client_app_traffic_secret,
352
0
                finsecret, hashlen))
353
0
            goto err;
354
0
        key = finsecret;
355
0
    }
356
357
22.4k
    if (!EVP_Q_mac(sctx->libctx, "HMAC", sctx->propq, mdname,
358
22.4k
            params, key, hashlen, hash, hashlen,
359
            /* outsize as per sizeof(peer_finish_md) */
360
22.4k
            out, EVP_MAX_MD_SIZE * 2, &len)) {
361
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
362
0
        goto err;
363
0
    }
364
365
22.4k
err:
366
22.4k
    OPENSSL_cleanse(finsecret, sizeof(finsecret));
367
22.4k
    return len;
368
22.4k
}
369
370
/*
371
 * There isn't really a key block in TLSv1.3, but we still need this function
372
 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
373
 */
374
int tls13_setup_key_block(SSL_CONNECTION *s)
375
4.97k
{
376
4.97k
    const EVP_CIPHER *c;
377
4.97k
    const EVP_CIPHER *snc = NULL, **p_snc = SSL_CONNECTION_IS_DTLS(s) ? &snc : NULL;
378
4.97k
    const EVP_MD *hash;
379
4.97k
    int mac_type = NID_undef;
380
4.97k
    size_t mac_secret_size = 0;
381
382
4.97k
    s->session->cipher = s->s3.tmp.new_cipher;
383
4.97k
    if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, p_snc, &c,
384
4.97k
            &hash, &mac_type, &mac_secret_size, NULL, 0)) {
385
        /* Error is already recorded */
386
0
        SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
387
0
        return 0;
388
0
    }
389
390
4.97k
    ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
391
4.97k
    s->s3.tmp.new_sym_enc = c;
392
4.97k
    ssl_evp_cipher_free(s->s3.tmp.new_sym_enc_sn);
393
4.97k
    s->s3.tmp.new_sym_enc_sn = snc;
394
4.97k
    ssl_evp_md_free(s->s3.tmp.new_hash);
395
4.97k
    s->s3.tmp.new_hash = hash;
396
4.97k
    s->s3.tmp.new_mac_pkey_type = mac_type;
397
4.97k
    s->s3.tmp.new_mac_secret_size = mac_secret_size;
398
399
4.97k
    return 1;
400
4.97k
}
401
402
static int derive_secret_key_and_iv(SSL_CONNECTION *s, const EVP_MD *md,
403
    const EVP_CIPHER *ciph,
404
    int mac_type,
405
    const EVP_MD *mac_md,
406
    const unsigned char *insecret,
407
    const unsigned char *hash,
408
    const unsigned char *label,
409
    size_t labellen, unsigned char *secret,
410
    unsigned char *snkey,
411
    unsigned char *key, size_t *keylen,
412
    unsigned char **iv, size_t *ivlen,
413
    size_t *taglen)
414
14.8k
{
415
14.8k
    int hashleni = EVP_MD_get_size(md);
416
14.8k
    size_t hashlen;
417
14.8k
    int mode, mac_mdleni;
418
419
    /* Ensure cast to size_t is safe */
420
14.8k
    if (!ossl_assert(hashleni > 0)) {
421
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
422
0
        return 0;
423
0
    }
424
14.8k
    hashlen = (size_t)hashleni;
425
426
14.8k
    if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
427
14.8k
            secret, hashlen, 1)) {
428
        /* SSLfatal() already called */
429
0
        return 0;
430
0
    }
431
432
    /* if ciph is NULL cipher, then use new_hash to calculate keylen */
433
14.8k
    if (EVP_CIPHER_is_a(ciph, "NULL")
434
0
        && mac_md != NULL
435
0
        && mac_type == NID_hmac) {
436
0
        mac_mdleni = EVP_MD_get_size(mac_md);
437
438
0
        if (mac_mdleni <= 0) {
439
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
440
0
            return 0;
441
0
        }
442
0
        *ivlen = *taglen = (size_t)mac_mdleni;
443
0
        *keylen = s->s3.tmp.new_mac_secret_size;
444
14.8k
    } else {
445
446
14.8k
        *keylen = EVP_CIPHER_get_key_length(ciph);
447
448
14.8k
        mode = EVP_CIPHER_get_mode(ciph);
449
14.8k
        if (mode == EVP_CIPH_CCM_MODE) {
450
0
            uint32_t algenc;
451
452
0
            *ivlen = EVP_CCM_TLS_IV_LEN;
453
0
            if (s->s3.tmp.new_cipher != NULL) {
454
0
                algenc = s->s3.tmp.new_cipher->algorithm_enc;
455
0
            } else if (s->session->cipher != NULL) {
456
                /* We've not selected a cipher yet - we must be doing early data */
457
0
                algenc = s->session->cipher->algorithm_enc;
458
0
            } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
459
                /* We must be doing early data with out-of-band PSK */
460
0
                algenc = s->psksession->cipher->algorithm_enc;
461
0
            } else {
462
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
463
0
                return 0;
464
0
            }
465
0
            if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
466
0
                *taglen = EVP_CCM8_TLS_TAG_LEN;
467
0
            else
468
0
                *taglen = EVP_CCM_TLS_TAG_LEN;
469
14.8k
        } else {
470
14.8k
            int iivlen;
471
472
14.8k
            if (mode == EVP_CIPH_GCM_MODE) {
473
13.3k
                *taglen = EVP_GCM_TLS_TAG_LEN;
474
13.3k
            } else {
475
                /* CHACHA20P-POLY1305 */
476
1.52k
                *taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
477
1.52k
            }
478
14.8k
            iivlen = EVP_CIPHER_get_iv_length(ciph);
479
14.8k
            if (iivlen < 0) {
480
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
481
0
                return 0;
482
0
            }
483
14.8k
            *ivlen = iivlen;
484
14.8k
        }
485
14.8k
    }
486
487
14.8k
    if (*ivlen > EVP_MAX_IV_LENGTH) {
488
0
        *iv = OPENSSL_malloc(*ivlen);
489
0
        if (*iv == NULL) {
490
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
491
0
            return 0;
492
0
        }
493
0
    }
494
495
14.8k
    if (!tls13_derive_key(s, md, secret, key, *keylen)
496
14.8k
        || !tls13_derive_iv(s, md, secret, *iv, *ivlen)
497
14.8k
        || (SSL_CONNECTION_IS_DTLS(s)
498
0
            && !dtls13_derive_snkey(s, md, secret, snkey, *keylen))) {
499
        /* SSLfatal() already called */
500
0
        return 0;
501
0
    }
502
503
14.8k
    return 1;
504
14.8k
}
505
506
static int tls13_store_hash(SSL_CONNECTION *s, unsigned char *hash, size_t len)
507
28.3k
{
508
28.3k
    size_t hashlen;
509
510
28.3k
    if (!ssl3_digest_cached_records(s, 1)
511
28.3k
        || !ssl_handshake_hash(s, hash, len, &hashlen)) {
512
0
        /* SSLfatal() already called */;
513
0
        return 0;
514
0
    }
515
516
28.3k
    return 1;
517
28.3k
}
518
519
int tls13_store_handshake_traffic_hash(SSL_CONNECTION *s)
520
18.5k
{
521
18.5k
    return tls13_store_hash(s, s->handshake_traffic_hash,
522
18.5k
        sizeof(s->handshake_traffic_hash));
523
18.5k
}
524
525
int tls13_store_server_finished_hash(SSL_CONNECTION *s)
526
9.82k
{
527
9.82k
    return tls13_store_hash(s, s->server_finished_hash,
528
9.82k
        sizeof(s->server_finished_hash));
529
9.82k
}
530
531
int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
532
14.8k
{
533
    /* ASCII: "c e traffic", in hex for EBCDIC compatibility */
534
14.8k
    static const unsigned char client_early_traffic[] = "\x63\x20\x65\x20\x74\x72\x61\x66\x66\x69\x63";
535
    /* ASCII: "c hs traffic", in hex for EBCDIC compatibility */
536
14.8k
    static const unsigned char client_handshake_traffic[] = "\x63\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
537
    /* ASCII: "c ap traffic", in hex for EBCDIC compatibility */
538
14.8k
    static const unsigned char client_application_traffic[] = "\x63\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
539
    /* ASCII: "s hs traffic", in hex for EBCDIC compatibility */
540
14.8k
    static const unsigned char server_handshake_traffic[] = "\x73\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
541
    /* ASCII: "s ap traffic", in hex for EBCDIC compatibility */
542
14.8k
    static const unsigned char server_application_traffic[] = "\x73\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
543
    /* ASCII: "exp master", in hex for EBCDIC compatibility */
544
14.8k
    static const unsigned char exporter_master_secret[] = "\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
545
    /* ASCII: "res master", in hex for EBCDIC compatibility */
546
14.8k
    static const unsigned char resumption_master_secret[] = "\x72\x65\x73\x20\x6D\x61\x73\x74\x65\x72";
547
    /* ASCII: "e exp master", in hex for EBCDIC compatibility */
548
14.8k
    static const unsigned char early_exporter_master_secret[] = "\x65\x20\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
549
14.8k
    unsigned char iv_intern[EVP_MAX_IV_LENGTH];
550
14.8k
    unsigned char *iv = iv_intern;
551
14.8k
    unsigned char key[EVP_MAX_KEY_LENGTH];
552
14.8k
    unsigned char snkey[EVP_MAX_KEY_LENGTH];
553
14.8k
    unsigned char secret[EVP_MAX_MD_SIZE];
554
14.8k
    unsigned char hashval[EVP_MAX_MD_SIZE];
555
14.8k
    unsigned char *hash = hashval;
556
14.8k
    unsigned char *insecret;
557
14.8k
    unsigned char *finsecret = NULL;
558
14.8k
    const char *log_label = NULL;
559
14.8k
    int finsecretlen = 0;
560
14.8k
    const unsigned char *label;
561
14.8k
    size_t labellen, hashlen = 0;
562
14.8k
    int ret = 0;
563
14.8k
    const EVP_MD *md = NULL, *mac_md = NULL;
564
14.8k
    const EVP_CIPHER *cipher = NULL, *sncipher = NULL;
565
14.8k
    int mac_pkey_type = NID_undef;
566
14.8k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
567
14.8k
    size_t keylen, ivlen = EVP_MAX_IV_LENGTH, taglen;
568
14.8k
    int level;
569
14.8k
    int direction = (which & SSL3_CC_READ) != 0 ? OSSL_RECORD_DIRECTION_READ
570
14.8k
                                                : OSSL_RECORD_DIRECTION_WRITE;
571
572
14.8k
    if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
573
8.30k
        || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
574
7.10k
        if ((which & SSL3_CC_EARLY) != 0) {
575
0
            EVP_MD_CTX *mdctx = NULL;
576
0
            long handlen;
577
0
            void *hdata;
578
0
            unsigned int hashlenui;
579
0
            const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
580
581
0
            insecret = s->early_secret;
582
0
            label = client_early_traffic;
583
0
            labellen = sizeof(client_early_traffic) - 1;
584
0
            log_label = CLIENT_EARLY_LABEL;
585
586
0
#ifndef OPENSSL_NO_ECH
587
            /* if ECH worked then use the innerch and not the h/s buffer here */
588
0
            if (((which & SSL3_CC_SERVER) && s->ext.ech.success == 1)
589
0
                || ((which & SSL3_CC_CLIENT) && s->ext.ech.attempted == 1)) {
590
0
                if (s->ext.ech.innerch == NULL) {
591
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
592
0
                    goto err;
593
0
                }
594
0
                handlen = (long)s->ext.ech.innerch_len;
595
0
                hdata = s->ext.ech.innerch;
596
0
            } else
597
0
#endif
598
0
            {
599
0
                handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
600
0
                if (handlen <= 0) {
601
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR,
602
0
                        SSL_R_BAD_HANDSHAKE_LENGTH);
603
0
                    goto err;
604
0
                }
605
0
            }
606
607
            /*
608
             * 0-RTT keys off the frozen slot-0 PSK (candidate_at(0)), which
609
             * may be the external psksession rather than s->session.
610
             */
611
0
            if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
612
0
                && s->ext.early_data_session != NULL) {
613
0
                if (!ossl_assert(s->max_early_data
614
0
                        == s->ext.early_data_session->ext.max_early_data)) {
615
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
616
0
                    goto err;
617
0
                }
618
0
                sslcipher = SSL_SESSION_get0_cipher(s->ext.early_data_session);
619
0
            }
620
0
            if (sslcipher == NULL) {
621
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
622
0
                goto err;
623
0
            }
624
625
            /*
626
             * This ups the ref count on cipher so we better make sure we free
627
             * it again
628
             */
629
0
            if (!ssl_cipher_get_evp_cipher(sctx, sslcipher, &cipher)
630
0
                || (SSL_CONNECTION_IS_DTLS(s)
631
0
                    && !ssl_cipher_get_evp_cipher_sn(sctx, sslcipher, &sncipher))) {
632
                /* Error is already recorded */
633
0
                SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
634
0
                goto err;
635
0
            }
636
637
0
            if (((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
638
0
                && (!ssl_cipher_get_evp_md_mac(sctx, sslcipher, &mac_md,
639
0
                    &mac_pkey_type, NULL))) {
640
0
                SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
641
0
                goto err;
642
0
            }
643
644
            /*
645
             * We need to calculate the handshake digest using the digest from
646
             * the session. We haven't yet selected our ciphersuite so we can't
647
             * use ssl_handshake_md().
648
             */
649
0
            mdctx = EVP_MD_CTX_new();
650
0
            if (mdctx == NULL) {
651
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
652
0
                goto err;
653
0
            }
654
655
0
            md = ssl_md(sctx, sslcipher->algorithm2);
656
0
            if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)) {
657
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
658
0
                EVP_MD_CTX_free(mdctx);
659
0
                goto err;
660
0
            }
661
662
0
            if (SSL_CONNECTION_IS_DTLS(s)) {
663
0
                if (!dtls13_transcript_hash_update(mdctx, hdata,
664
0
                        (size_t)handlen)) {
665
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
666
0
                    EVP_MD_CTX_free(mdctx);
667
0
                    goto err;
668
0
                }
669
0
            } else {
670
0
                if (!EVP_DigestUpdate(mdctx, hdata, handlen)) {
671
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
672
0
                    EVP_MD_CTX_free(mdctx);
673
0
                    goto err;
674
0
                }
675
0
            }
676
677
0
            if (!EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
678
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
679
0
                EVP_MD_CTX_free(mdctx);
680
0
                goto err;
681
0
            }
682
0
            hashlen = hashlenui;
683
0
            EVP_MD_CTX_free(mdctx);
684
685
0
            if (!tls13_hkdf_expand(s, md, insecret,
686
0
                    early_exporter_master_secret,
687
0
                    sizeof(early_exporter_master_secret) - 1,
688
0
                    hashval, hashlen,
689
0
                    s->early_exporter_master_secret, hashlen,
690
0
                    1)) {
691
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
692
0
                goto err;
693
0
            }
694
695
0
            if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
696
0
                    s->early_exporter_master_secret, hashlen)) {
697
                /* SSLfatal() already called */
698
0
                goto err;
699
0
            }
700
7.10k
        } else if (which & SSL3_CC_HANDSHAKE) {
701
4.89k
            insecret = s->handshake_secret;
702
4.89k
            finsecret = s->client_finished_secret;
703
4.89k
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
704
4.89k
            if (finsecretlen <= 0) {
705
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
706
0
                goto err;
707
0
            }
708
4.89k
            label = client_handshake_traffic;
709
4.89k
            labellen = sizeof(client_handshake_traffic) - 1;
710
4.89k
            log_label = CLIENT_HANDSHAKE_LABEL;
711
            /*
712
             * The handshake hash used for the server read/client write handshake
713
             * traffic secret is the same as the hash for the server
714
             * write/client read handshake traffic secret. However, if we
715
             * processed early data then we delay changing the server
716
             * read/client write cipher state until later, and the handshake
717
             * hashes have moved on. Therefore we use the value saved earlier
718
             * when we did the server write/client read change cipher state.
719
             */
720
4.89k
            hash = s->handshake_traffic_hash;
721
4.89k
        } else {
722
2.20k
            insecret = s->master_secret;
723
2.20k
            label = client_application_traffic;
724
2.20k
            labellen = sizeof(client_application_traffic) - 1;
725
2.20k
            log_label = CLIENT_APPLICATION_LABEL;
726
            /*
727
             * For this we only use the handshake hashes up until the server
728
             * Finished hash. We do not include the client's Finished, which is
729
             * what ssl_handshake_hash() would give us. Instead we use the
730
             * previously saved value.
731
             */
732
2.20k
            hash = s->server_finished_hash;
733
2.20k
        }
734
7.74k
    } else {
735
        /* Early data never applies to client-read/server-write */
736
7.74k
        if (which & SSL3_CC_HANDSHAKE) {
737
4.97k
            insecret = s->handshake_secret;
738
4.97k
            finsecret = s->server_finished_secret;
739
4.97k
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
740
4.97k
            if (finsecretlen <= 0) {
741
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
742
0
                goto err;
743
0
            }
744
4.97k
            label = server_handshake_traffic;
745
4.97k
            labellen = sizeof(server_handshake_traffic) - 1;
746
4.97k
            log_label = SERVER_HANDSHAKE_LABEL;
747
4.97k
        } else {
748
2.76k
            insecret = s->master_secret;
749
2.76k
            label = server_application_traffic;
750
2.76k
            labellen = sizeof(server_application_traffic) - 1;
751
2.76k
            log_label = SERVER_APPLICATION_LABEL;
752
2.76k
            hash = s->server_finished_hash;
753
2.76k
        }
754
7.74k
    }
755
756
14.8k
    if ((which & SSL3_CC_EARLY) == 0) {
757
14.8k
        md = ssl_handshake_md(s);
758
14.8k
        cipher = s->s3.tmp.new_sym_enc;
759
14.8k
        mac_md = s->s3.tmp.new_hash;
760
14.8k
        mac_pkey_type = s->s3.tmp.new_mac_pkey_type;
761
762
14.8k
        if (SSL_CONNECTION_IS_DTLS(s)) {
763
0
            sncipher = s->s3.tmp.new_sym_enc_sn;
764
0
        }
765
14.8k
        if (!ssl3_digest_cached_records(s, 1)
766
14.8k
            || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
767
0
            /* SSLfatal() already called */;
768
0
            goto err;
769
0
        }
770
14.8k
    }
771
772
14.8k
    if (label == client_application_traffic) {
773
        /*
774
         * We also create the resumption master secret, but this time use the
775
         * hash for the whole handshake including the Client Finished
776
         */
777
2.20k
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
778
2.20k
                resumption_master_secret,
779
2.20k
                sizeof(resumption_master_secret) - 1,
780
2.20k
                hashval, hashlen, s->resumption_master_secret,
781
2.20k
                hashlen, 1)) {
782
            /* SSLfatal() already called */
783
0
            goto err;
784
0
        }
785
2.20k
    }
786
787
    /* check whether cipher is known */
788
14.8k
    if (!ossl_assert(cipher != NULL))
789
0
        goto err;
790
791
14.8k
    if (!derive_secret_key_and_iv(s, md, cipher, mac_pkey_type, mac_md,
792
14.8k
            insecret, hash, label, labellen, secret,
793
14.8k
            snkey, key, &keylen, &iv, &ivlen, &taglen)) {
794
        /* SSLfatal() already called */
795
0
        goto err;
796
0
    }
797
798
14.8k
    if (label == server_application_traffic) {
799
2.76k
        memcpy(s->server_app_traffic_secret, secret, hashlen);
800
        /* Now we create the exporter master secret */
801
2.76k
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
802
2.76k
                exporter_master_secret,
803
2.76k
                sizeof(exporter_master_secret) - 1,
804
2.76k
                hash, hashlen, s->exporter_master_secret,
805
2.76k
                hashlen, 1)) {
806
            /* SSLfatal() already called */
807
0
            goto err;
808
0
        }
809
810
2.76k
        if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
811
2.76k
                hashlen)) {
812
            /* SSLfatal() already called */
813
0
            goto err;
814
0
        }
815
12.0k
    } else if (label == client_application_traffic)
816
2.20k
        memcpy(s->client_app_traffic_secret, secret, hashlen);
817
818
14.8k
    if (!ssl_log_secret(s, log_label, secret, hashlen)) {
819
        /* SSLfatal() already called */
820
0
        goto err;
821
0
    }
822
823
14.8k
    if (finsecret != NULL
824
9.87k
        && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
825
9.87k
            finsecret, (size_t)finsecretlen)) {
826
        /* SSLfatal() already called */
827
0
        goto err;
828
0
    }
829
830
14.8k
    if ((which & SSL3_CC_WRITE) != 0) {
831
7.66k
        if (!s->server && label == client_early_traffic)
832
0
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 1);
833
7.66k
        else
834
7.66k
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 0);
835
7.66k
    }
836
837
14.8k
    level = (which & SSL3_CC_EARLY) != 0
838
14.8k
        ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
839
14.8k
        : ((which & SSL3_CC_HANDSHAKE) != 0
840
14.8k
                  ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
841
14.8k
                  : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
842
843
14.8k
    if (SSL_CONNECTION_IS_DTLS(s)) {
844
        /*
845
         * For DTLS1.3 The Compressed Certificate should still be sent in Epoch 2
846
         * not Epoch 3.
847
         */
848
0
        if (s->version != DTLS1_3_VERSION || (which & SSL3_CC_COMP_CERT) == 0) {
849
0
            if (!dtls1_increment_epoch(s, which)) {
850
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
851
0
                goto err;
852
0
            }
853
0
        }
854
855
0
        if (level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE && dtls1_get_epoch(s, which) == 1) {
856
            /*
857
             * We must manually increment epoch because
858
             * client early traffic was not sent/recv
859
             */
860
0
            if (!dtls1_increment_epoch(s, which)) {
861
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
862
0
                goto err;
863
0
            }
864
0
        }
865
866
        /* We have moved to the next flight lets clear out old messages */
867
0
        if (direction == OSSL_RECORD_DIRECTION_READ)
868
0
            dtls1_clear_received_buffer(s);
869
870
0
        dtls1_clear_sent_buffer(s, 1);
871
0
    }
872
873
14.8k
    if (!ssl_set_new_record_layer(s, s->version, direction, level, secret,
874
14.8k
            hashlen, snkey, key, keylen, iv, ivlen,
875
14.8k
            NULL, 0, sncipher, cipher, taglen,
876
14.8k
            mac_pkey_type, mac_md, NULL, md)) {
877
        /* SSLfatal already called */
878
5
        goto err;
879
5
    }
880
881
14.8k
    ret = 1;
882
14.8k
err:
883
14.8k
    if ((which & SSL3_CC_EARLY) != 0) {
884
        /* We up-refed this so now we need to down ref */
885
0
        if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
886
0
            ssl_evp_md_free(mac_md);
887
0
        ssl_evp_cipher_free(cipher);
888
0
        ssl_evp_cipher_free(sncipher);
889
0
    }
890
14.8k
    OPENSSL_cleanse(key, sizeof(key));
891
14.8k
    OPENSSL_cleanse(snkey, sizeof(snkey));
892
14.8k
    OPENSSL_cleanse(secret, sizeof(secret));
893
14.8k
    if (iv != iv_intern)
894
0
        OPENSSL_free(iv);
895
14.8k
    return ret;
896
14.8k
}
897
898
int tls13_update_key(SSL_CONNECTION *s, int sending)
899
0
{
900
    /* ASCII: "traffic upd", in hex for EBCDIC compatibility */
901
0
    static const unsigned char application_traffic[] = "\x74\x72\x61\x66\x66\x69\x63\x20\x75\x70\x64";
902
0
    const EVP_MD *md = ssl_handshake_md(s);
903
0
    size_t hashlen;
904
0
    unsigned char key[EVP_MAX_KEY_LENGTH];
905
0
    unsigned char snkey[EVP_MAX_KEY_LENGTH];
906
0
    const EVP_CIPHER *snenc = NULL;
907
0
    unsigned char *insecret;
908
0
    unsigned char secret[EVP_MAX_MD_SIZE];
909
0
    char *log_label;
910
0
    size_t keylen, ivlen, taglen;
911
0
    int ret = 0, l;
912
0
    int direction = sending ? OSSL_RECORD_DIRECTION_WRITE
913
0
                            : OSSL_RECORD_DIRECTION_READ;
914
0
    int which = sending ? SSL3_CC_WRITE : SSL3_CC_READ;
915
0
    unsigned char iv_intern[EVP_MAX_IV_LENGTH];
916
0
    unsigned char *iv = iv_intern;
917
918
0
    if ((l = EVP_MD_get_size(md)) <= 0) {
919
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
920
0
        return 0;
921
0
    }
922
0
    hashlen = (size_t)l;
923
924
0
    if (s->server == sending)
925
0
        insecret = s->server_app_traffic_secret;
926
0
    else
927
0
        insecret = s->client_app_traffic_secret;
928
929
0
    if (!derive_secret_key_and_iv(s, md,
930
0
            s->s3.tmp.new_sym_enc,
931
0
            s->s3.tmp.new_mac_pkey_type, s->s3.tmp.new_hash,
932
0
            insecret, NULL,
933
0
            application_traffic,
934
0
            sizeof(application_traffic) - 1, secret, snkey,
935
0
            key, &keylen, &iv, &ivlen, &taglen)) {
936
        /* SSLfatal() already called */
937
0
        goto err;
938
0
    }
939
940
0
    memcpy(insecret, secret, hashlen);
941
942
0
    if (SSL_CONNECTION_IS_DTLS(s)) {
943
0
        if (!dtls1_increment_epoch(s, which)) {
944
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
945
0
            goto err;
946
0
        }
947
0
        snenc = s->s3.tmp.new_sym_enc_sn;
948
0
    }
949
950
0
    if (!ssl_set_new_record_layer(s, s->version, direction,
951
0
            OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
952
0
            insecret, hashlen, snkey, key, keylen,
953
0
            iv, ivlen, NULL, 0,
954
0
            snenc,
955
0
            s->s3.tmp.new_sym_enc,
956
0
            taglen, NID_undef, NULL, NULL, md)) {
957
        /* SSLfatal already called */
958
0
        goto err;
959
0
    }
960
961
    /* Call Key log on successful traffic secret update */
962
0
    log_label = s->server == sending ? SERVER_APPLICATION_N_LABEL : CLIENT_APPLICATION_N_LABEL;
963
0
    if (!ssl_log_secret(s, log_label, secret, hashlen)) {
964
        /* SSLfatal() already called */
965
0
        goto err;
966
0
    }
967
0
    ret = 1;
968
0
err:
969
0
    OPENSSL_cleanse(key, sizeof(key));
970
0
    OPENSSL_cleanse(snkey, sizeof(snkey));
971
0
    OPENSSL_cleanse(secret, sizeof(secret));
972
0
    if (iv != iv_intern)
973
0
        OPENSSL_free(iv);
974
0
    return ret;
975
0
}
976
977
int tls13_alert_code(int code)
978
12.5k
{
979
    /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
980
12.5k
    if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
981
223
        return code;
982
983
12.3k
    return tls1_alert_code(code);
984
12.5k
}
985
986
int tls13_export_keying_material(SSL_CONNECTION *s,
987
    unsigned char *out, size_t olen,
988
    const char *label, size_t llen,
989
    const unsigned char *context,
990
    size_t contextlen, int use_context)
991
0
{
992
0
    unsigned char exportsecret[EVP_MAX_MD_SIZE];
993
    /* ASCII: "exporter", in hex for EBCDIC compatibility */
994
0
    static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
995
0
    unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
996
0
    const EVP_MD *md = ssl_handshake_md(s);
997
0
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
998
0
    unsigned int hashsize, datalen;
999
0
    int ret = 0;
1000
1001
0
    if (ctx == NULL || md == NULL || !ossl_statem_export_allowed(s))
1002
0
        goto err;
1003
1004
0
    if (!use_context)
1005
0
        contextlen = 0;
1006
1007
0
    if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
1008
0
        || EVP_DigestUpdate(ctx, context, contextlen) <= 0
1009
0
        || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
1010
0
        || EVP_DigestInit_ex(ctx, md, NULL) <= 0
1011
0
        || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
1012
0
        || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
1013
0
            (const unsigned char *)label, llen,
1014
0
            data, datalen, exportsecret, hashsize, 0)
1015
0
        || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
1016
0
            sizeof(exporterlabel) - 1, hash, hashsize,
1017
0
            out, olen, 0))
1018
0
        goto err;
1019
1020
0
    ret = 1;
1021
0
err:
1022
0
    EVP_MD_CTX_free(ctx);
1023
0
    return ret;
1024
0
}
1025
1026
int tls13_export_keying_material_early(SSL_CONNECTION *s,
1027
    unsigned char *out, size_t olen,
1028
    const char *label, size_t llen,
1029
    const unsigned char *context,
1030
    size_t contextlen)
1031
0
{
1032
    /* ASCII: "exporter", in hex for EBCDIC compatibility */
1033
0
    static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
1034
0
    unsigned char exportsecret[EVP_MAX_MD_SIZE];
1035
0
    unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
1036
0
    const EVP_MD *md;
1037
0
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1038
0
    unsigned int hashsize, datalen;
1039
0
    int ret = 0;
1040
0
    const SSL_CIPHER *sslcipher;
1041
1042
0
    if (ctx == NULL || !ossl_statem_export_early_allowed(s))
1043
0
        goto err;
1044
1045
0
    if (!s->server && s->ext.early_data_session != NULL)
1046
0
        sslcipher = SSL_SESSION_get0_cipher(s->ext.early_data_session);
1047
0
    else
1048
0
        sslcipher = SSL_SESSION_get0_cipher(s->session);
1049
1050
0
    md = ssl_md(SSL_CONNECTION_GET_CTX(s), sslcipher->algorithm2);
1051
1052
    /*
1053
     * Calculate the hash value and store it in |data|. The reason why
1054
     * the empty string is used is that the definition of TLS-Exporter
1055
     * is like so:
1056
     *
1057
     * TLS-Exporter(label, context_value, key_length) =
1058
     *     HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
1059
     *                       "exporter", Hash(context_value), key_length)
1060
     *
1061
     * Derive-Secret(Secret, Label, Messages) =
1062
     *       HKDF-Expand-Label(Secret, Label,
1063
     *                         Transcript-Hash(Messages), Hash.length)
1064
     *
1065
     * Here Transcript-Hash is the cipher suite hash algorithm.
1066
     */
1067
0
    if (md == NULL
1068
0
        || EVP_DigestInit_ex(ctx, md, NULL) <= 0
1069
0
        || EVP_DigestUpdate(ctx, context, contextlen) <= 0
1070
0
        || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
1071
0
        || EVP_DigestInit_ex(ctx, md, NULL) <= 0
1072
0
        || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
1073
0
        || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
1074
0
            (const unsigned char *)label, llen,
1075
0
            data, datalen, exportsecret, hashsize, 0)
1076
0
        || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
1077
0
            sizeof(exporterlabel) - 1, hash, hashsize,
1078
0
            out, olen, 0))
1079
0
        goto err;
1080
1081
0
    ret = 1;
1082
0
err:
1083
0
    EVP_MD_CTX_free(ctx);
1084
0
    return ret;
1085
0
}