Coverage Report

Created: 2025-12-31 06:58

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