Coverage Report

Created: 2025-12-31 06:58

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