Coverage Report

Created: 2026-08-31 06:56

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-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
0
#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
0
{
44
0
    EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_TLS1_3_KDF, propq);
45
0
    EVP_KDF_CTX *kctx;
46
0
    OSSL_PARAM params[8], *p = params;
47
0
    int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
48
0
    const char *mdname = EVP_MD_get0_name(md);
49
0
    int ret;
50
0
    size_t hashlen;
51
52
0
    kctx = EVP_KDF_CTX_new(kdf);
53
0
    EVP_KDF_free(kdf);
54
0
    if (kctx == NULL)
55
0
        return 0;
56
57
0
    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
0
    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
0
    hashlen = (size_t)ret;
76
77
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
78
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
79
0
        (char *)mdname, 0);
80
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
81
0
        (unsigned char *)secret, hashlen);
82
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
83
0
        (unsigned char *)label_prefix,
84
0
        label_prefix_len);
85
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
86
0
        (unsigned char *)label, labellen);
87
0
    if (data != NULL)
88
0
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_DATA,
89
0
            (unsigned char *)data,
90
0
            datalen);
91
0
    if (propq != NULL)
92
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_PROPERTIES,
93
0
            (char *)propq, 0);
94
95
0
    *p++ = OSSL_PARAM_construct_end();
96
97
0
    ret = EVP_KDF_derive(kctx, out, outlen, params) <= 0;
98
0
    EVP_KDF_CTX_free(kctx);
99
100
0
    if (ret != 0) {
101
0
        if (raise_error)
102
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
103
0
    }
104
105
0
    return ret == 0;
106
0
}
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
0
{
115
    /* This function only supports TLSv1.3 and not DTLSv1.3 */
116
0
    return hkdf_expand(libctx, propq, md, secret, label_prefix_tls13,
117
0
        sizeof(label_prefix_tls13) - 1,
118
0
        label, labellen, data, datalen, out, outlen,
119
0
        raise_error);
120
0
}
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
0
{
128
0
    int ret;
129
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
130
0
    const int isdtls = SSL_CONNECTION_IS_DTLS(s);
131
0
    const unsigned char *label_prefix = isdtls ? label_prefix_dtls13
132
0
                                               : label_prefix_tls13;
133
0
    const size_t label_prefix_len = isdtls ? sizeof(label_prefix_dtls13) - 1
134
0
                                           : sizeof(label_prefix_tls13) - 1;
135
136
0
    ret = hkdf_expand(sctx->libctx, sctx->propq, md, secret, label_prefix,
137
0
        label_prefix_len, label, labellen, data,
138
0
        datalen, out, outlen, !fatal);
139
0
    if (ret == 0 && fatal)
140
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
141
142
0
    return ret;
143
0
}
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
0
{
153
    /* ASCII: "key", in hex for EBCDIC compatibility */
154
0
    static const unsigned char keylabel[] = "\x6B\x65\x79";
155
156
0
    return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
157
0
        NULL, 0, key, keylen, 1);
158
0
}
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
0
{
168
    /* ASCII: "iv", in hex for EBCDIC compatibility */
169
0
    static const unsigned char ivlabel[] = "\x69\x76";
170
171
0
    return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
172
0
        NULL, 0, iv, ivlen, 1);
173
0
}
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
0
{
179
    /* ASCII: "finished", in hex for EBCDIC compatibility */
180
0
    static const unsigned char finishedlabel[] = "\x66\x69\x6E\x69\x73\x68\x65\x64";
181
182
0
    return tls13_hkdf_expand(s, md, secret, finishedlabel,
183
0
        sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
184
0
}
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
0
{
212
0
    size_t mdlen;
213
0
    int mdleni;
214
0
    int ret;
215
0
    EVP_KDF *kdf;
216
0
    EVP_KDF_CTX *kctx;
217
0
    OSSL_PARAM params[7], *p = params;
218
0
    int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
219
0
    const char *mdname = EVP_MD_get0_name(md);
220
    /* ASCII: "derived", in hex for EBCDIC compatibility */
221
0
    static const char derived_secret_label[] = "\x64\x65\x72\x69\x76\x65\x64";
222
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
223
0
    int isdtls = SSL_CONNECTION_IS_DTLS(s);
224
225
0
    kdf = EVP_KDF_fetch(sctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF, sctx->propq);
226
0
    kctx = EVP_KDF_CTX_new(kdf);
227
0
    EVP_KDF_free(kdf);
228
0
    if (kctx == NULL) {
229
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
230
0
        return 0;
231
0
    }
232
233
0
    mdleni = EVP_MD_get_size(md);
234
    /* Ensure cast to size_t is safe */
235
0
    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
0
    mdlen = (size_t)mdleni;
241
242
0
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
243
0
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
244
0
        (char *)mdname, 0);
245
0
    if (insecret != NULL)
246
0
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
247
0
            (unsigned char *)insecret,
248
0
            insecretlen);
249
0
    if (prevsecret != NULL)
250
0
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
251
0
            (unsigned char *)prevsecret, mdlen);
252
0
    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
0
    else
257
0
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
258
0
            (unsigned char *)label_prefix_tls13,
259
0
            sizeof(label_prefix_tls13) - 1);
260
261
0
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
262
0
        (unsigned char *)derived_secret_label,
263
0
        sizeof(derived_secret_label) - 1);
264
0
    *p++ = OSSL_PARAM_construct_end();
265
266
0
    ret = EVP_KDF_derive(kctx, outsecret, mdlen, params) <= 0;
267
268
0
    if (ret != 0)
269
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
270
271
0
    EVP_KDF_CTX_free(kctx);
272
0
    return ret == 0;
273
0
}
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
0
{
284
    /* Calls SSLfatal() if required */
285
0
    return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
286
0
        insecret, insecretlen,
287
0
        (unsigned char *)&s->handshake_secret);
288
0
}
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
0
{
299
0
    const EVP_MD *md = ssl_handshake_md(s);
300
0
    int md_size;
301
302
0
    md_size = EVP_MD_get_size(md);
303
0
    if (md_size <= 0) {
304
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
305
0
        return 0;
306
0
    }
307
0
    *secret_size = (size_t)md_size;
308
    /* Calls SSLfatal() if required */
309
0
    return tls13_generate_secret(s, md, prev, NULL, 0, out);
310
0
}
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
0
{
319
0
    const EVP_MD *md = ssl_handshake_md(s);
320
0
    const char *mdname = EVP_MD_get0_name(md);
321
0
    unsigned char hash[EVP_MAX_MD_SIZE];
322
0
    unsigned char finsecret[EVP_MAX_MD_SIZE];
323
0
    unsigned char *key = NULL;
324
0
    size_t len = 0, hashlen;
325
0
    OSSL_PARAM params[2], *p = params;
326
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
327
328
0
    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
0
    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
0
    *p = OSSL_PARAM_construct_end();
339
340
0
    if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
341
        /* SSLfatal() already called */
342
0
        goto err;
343
0
    }
344
345
0
    if (str == SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->server_finished_label) {
346
0
        key = s->server_finished_secret;
347
0
    } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
348
0
        key = s->client_finished_secret;
349
0
    } 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
0
    if (!EVP_Q_mac(sctx->libctx, "HMAC", sctx->propq, mdname,
358
0
            params, key, hashlen, hash, hashlen,
359
            /* outsize as per sizeof(peer_finish_md) */
360
0
            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
0
err:
366
0
    OPENSSL_cleanse(finsecret, sizeof(finsecret));
367
0
    return len;
368
0
}
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
0
{
376
0
    const EVP_CIPHER *c;
377
0
    const EVP_CIPHER *snc = NULL, **p_snc = SSL_CONNECTION_IS_DTLS(s) ? &snc : NULL;
378
0
    const EVP_MD *hash;
379
0
    int mac_type = NID_undef;
380
0
    size_t mac_secret_size = 0;
381
382
0
    s->session->cipher = s->s3.tmp.new_cipher;
383
0
    if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, p_snc, &c,
384
0
            &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
0
    ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
391
0
    s->s3.tmp.new_sym_enc = c;
392
0
    ssl_evp_cipher_free(s->s3.tmp.new_sym_enc_sn);
393
0
    s->s3.tmp.new_sym_enc_sn = snc;
394
0
    ssl_evp_md_free(s->s3.tmp.new_hash);
395
0
    s->s3.tmp.new_hash = hash;
396
0
    s->s3.tmp.new_mac_pkey_type = mac_type;
397
0
    s->s3.tmp.new_mac_secret_size = mac_secret_size;
398
399
0
    return 1;
400
0
}
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
0
{
415
0
    int hashleni = EVP_MD_get_size(md);
416
0
    size_t hashlen;
417
0
    int mode, mac_mdleni;
418
419
    /* Ensure cast to size_t is safe */
420
0
    if (!ossl_assert(hashleni > 0)) {
421
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
422
0
        return 0;
423
0
    }
424
0
    hashlen = (size_t)hashleni;
425
426
0
    if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
427
0
            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
0
    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
0
    } else {
445
446
0
        *keylen = EVP_CIPHER_get_key_length(ciph);
447
448
0
        mode = EVP_CIPHER_get_mode(ciph);
449
0
        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
0
        } else {
470
0
            int iivlen;
471
472
0
            if (mode == EVP_CIPH_GCM_MODE) {
473
0
                *taglen = EVP_GCM_TLS_TAG_LEN;
474
0
            } else {
475
                /* CHACHA20P-POLY1305 */
476
0
                *taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
477
0
            }
478
0
            iivlen = EVP_CIPHER_get_iv_length(ciph);
479
0
            if (iivlen < 0) {
480
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
481
0
                return 0;
482
0
            }
483
0
            *ivlen = iivlen;
484
0
        }
485
0
    }
486
487
0
    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
0
    if (!tls13_derive_key(s, md, secret, key, *keylen)
496
0
        || !tls13_derive_iv(s, md, secret, *iv, *ivlen)
497
0
        || (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
0
    return 1;
504
0
}
505
506
static int tls13_store_hash(SSL_CONNECTION *s, unsigned char *hash, size_t len)
507
0
{
508
0
    size_t hashlen;
509
510
0
    if (!ssl3_digest_cached_records(s, 1)
511
0
        || !ssl_handshake_hash(s, hash, len, &hashlen)) {
512
0
        /* SSLfatal() already called */;
513
0
        return 0;
514
0
    }
515
516
0
    return 1;
517
0
}
518
519
int tls13_store_handshake_traffic_hash(SSL_CONNECTION *s)
520
0
{
521
0
    return tls13_store_hash(s, s->handshake_traffic_hash,
522
0
        sizeof(s->handshake_traffic_hash));
523
0
}
524
525
int tls13_store_server_finished_hash(SSL_CONNECTION *s)
526
0
{
527
0
    return tls13_store_hash(s, s->server_finished_hash,
528
0
        sizeof(s->server_finished_hash));
529
0
}
530
531
int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
532
0
{
533
    /* ASCII: "c e traffic", in hex for EBCDIC compatibility */
534
0
    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
0
    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
0
    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
0
    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
0
    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
0
    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
0
    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
0
    static const unsigned char early_exporter_master_secret[] = "\x65\x20\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
549
0
    unsigned char iv_intern[EVP_MAX_IV_LENGTH];
550
0
    unsigned char *iv = iv_intern;
551
0
    unsigned char key[EVP_MAX_KEY_LENGTH];
552
0
    unsigned char snkey[EVP_MAX_KEY_LENGTH];
553
0
    unsigned char secret[EVP_MAX_MD_SIZE];
554
0
    unsigned char hashval[EVP_MAX_MD_SIZE];
555
0
    unsigned char *hash = hashval;
556
0
    unsigned char *insecret;
557
0
    unsigned char *finsecret = NULL;
558
0
    const char *log_label = NULL;
559
0
    int finsecretlen = 0;
560
0
    const unsigned char *label;
561
0
    size_t labellen, hashlen = 0;
562
0
    int ret = 0;
563
0
    const EVP_MD *md = NULL, *mac_md = NULL;
564
0
    const EVP_CIPHER *cipher = NULL, *sncipher = NULL;
565
0
    int mac_pkey_type = NID_undef;
566
0
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
567
0
    size_t keylen, ivlen = EVP_MAX_IV_LENGTH, taglen;
568
0
    int level;
569
0
    int direction = (which & SSL3_CC_READ) != 0 ? OSSL_RECORD_DIRECTION_READ
570
0
                                                : OSSL_RECORD_DIRECTION_WRITE;
571
572
0
    if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
573
0
        || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
574
0
        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
0
        } else if (which & SSL3_CC_HANDSHAKE) {
701
0
            insecret = s->handshake_secret;
702
0
            finsecret = s->client_finished_secret;
703
0
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
704
0
            if (finsecretlen <= 0) {
705
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
706
0
                goto err;
707
0
            }
708
0
            label = client_handshake_traffic;
709
0
            labellen = sizeof(client_handshake_traffic) - 1;
710
0
            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
0
            hash = s->handshake_traffic_hash;
721
0
        } else {
722
0
            insecret = s->master_secret;
723
0
            label = client_application_traffic;
724
0
            labellen = sizeof(client_application_traffic) - 1;
725
0
            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
0
            hash = s->server_finished_hash;
733
0
        }
734
0
    } else {
735
        /* Early data never applies to client-read/server-write */
736
0
        if (which & SSL3_CC_HANDSHAKE) {
737
0
            insecret = s->handshake_secret;
738
0
            finsecret = s->server_finished_secret;
739
0
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
740
0
            if (finsecretlen <= 0) {
741
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
742
0
                goto err;
743
0
            }
744
0
            label = server_handshake_traffic;
745
0
            labellen = sizeof(server_handshake_traffic) - 1;
746
0
            log_label = SERVER_HANDSHAKE_LABEL;
747
0
        } else {
748
0
            insecret = s->master_secret;
749
0
            label = server_application_traffic;
750
0
            labellen = sizeof(server_application_traffic) - 1;
751
0
            log_label = SERVER_APPLICATION_LABEL;
752
0
            hash = s->server_finished_hash;
753
0
        }
754
0
    }
755
756
0
    if ((which & SSL3_CC_EARLY) == 0) {
757
0
        md = ssl_handshake_md(s);
758
0
        cipher = s->s3.tmp.new_sym_enc;
759
0
        mac_md = s->s3.tmp.new_hash;
760
0
        mac_pkey_type = s->s3.tmp.new_mac_pkey_type;
761
762
0
        if (SSL_CONNECTION_IS_DTLS(s)) {
763
0
            sncipher = s->s3.tmp.new_sym_enc_sn;
764
0
        }
765
0
        if (!ssl3_digest_cached_records(s, 1)
766
0
            || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
767
0
            /* SSLfatal() already called */;
768
0
            goto err;
769
0
        }
770
0
    }
771
772
0
    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
0
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
778
0
                resumption_master_secret,
779
0
                sizeof(resumption_master_secret) - 1,
780
0
                hashval, hashlen, s->resumption_master_secret,
781
0
                hashlen, 1)) {
782
            /* SSLfatal() already called */
783
0
            goto err;
784
0
        }
785
0
    }
786
787
    /* check whether cipher is known */
788
0
    if (!ossl_assert(cipher != NULL))
789
0
        goto err;
790
791
0
    if (!derive_secret_key_and_iv(s, md, cipher, mac_pkey_type, mac_md,
792
0
            insecret, hash, label, labellen, secret,
793
0
            snkey, key, &keylen, &iv, &ivlen, &taglen)) {
794
        /* SSLfatal() already called */
795
0
        goto err;
796
0
    }
797
798
0
    if (label == server_application_traffic) {
799
0
        memcpy(s->server_app_traffic_secret, secret, hashlen);
800
        /* Now we create the exporter master secret */
801
0
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
802
0
                exporter_master_secret,
803
0
                sizeof(exporter_master_secret) - 1,
804
0
                hash, hashlen, s->exporter_master_secret,
805
0
                hashlen, 1)) {
806
            /* SSLfatal() already called */
807
0
            goto err;
808
0
        }
809
810
0
        if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
811
0
                hashlen)) {
812
            /* SSLfatal() already called */
813
0
            goto err;
814
0
        }
815
0
    } else if (label == client_application_traffic)
816
0
        memcpy(s->client_app_traffic_secret, secret, hashlen);
817
818
0
    if (!ssl_log_secret(s, log_label, secret, hashlen)) {
819
        /* SSLfatal() already called */
820
0
        goto err;
821
0
    }
822
823
0
    if (finsecret != NULL
824
0
        && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
825
0
            finsecret, (size_t)finsecretlen)) {
826
        /* SSLfatal() already called */
827
0
        goto err;
828
0
    }
829
830
0
    if ((which & SSL3_CC_WRITE) != 0) {
831
0
        if (!s->server && label == client_early_traffic)
832
0
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 1);
833
0
        else
834
0
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 0);
835
0
    }
836
837
0
    level = (which & SSL3_CC_EARLY) != 0
838
0
        ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
839
0
        : ((which & SSL3_CC_HANDSHAKE) != 0
840
0
                  ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
841
0
                  : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
842
843
0
    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
0
    if (!ssl_set_new_record_layer(s, s->version, direction, level, secret,
874
0
            hashlen, snkey, key, keylen, iv, ivlen,
875
0
            NULL, 0, sncipher, cipher, taglen,
876
0
            mac_pkey_type, mac_md, NULL, md)) {
877
        /* SSLfatal already called */
878
0
        goto err;
879
0
    }
880
881
0
    ret = 1;
882
0
err:
883
0
    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
0
    OPENSSL_cleanse(key, sizeof(key));
891
0
    OPENSSL_cleanse(snkey, sizeof(snkey));
892
0
    OPENSSL_cleanse(secret, sizeof(secret));
893
0
    if (iv != iv_intern)
894
0
        OPENSSL_free(iv);
895
0
    return ret;
896
0
}
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
0
{
979
    /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
980
0
    if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
981
0
        return code;
982
983
0
    return tls1_alert_code(code);
984
0
}
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
}