Coverage Report

Created: 2026-10-16 07:31

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
            /*
587
             * Client: never install early-write keys from an underived (zero)
588
             * s->early_secret. It is set only when the first-offered PSK's
589
             * binder derived it this ClientHello flight (see
590
             * s->ext.early_secret_derived). If it is not set, the PSK offer and
591
             * the early_data decision diverged; fail closed rather than protect
592
             * 0-RTT under a secret a passive observer could reconstruct.
593
             */
594
0
            if ((which & SSL3_CC_CLIENT) != 0 && !s->ext.early_secret_derived) {
595
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
596
0
                goto err;
597
0
            }
598
599
0
#ifndef OPENSSL_NO_ECH
600
            /* if ECH worked then use the innerch and not the h/s buffer here */
601
0
            if (((which & SSL3_CC_SERVER) && s->ext.ech.success == 1)
602
0
                || ((which & SSL3_CC_CLIENT) && s->ext.ech.attempted == 1)) {
603
0
                if (s->ext.ech.innerch == NULL) {
604
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
605
0
                    goto err;
606
0
                }
607
0
                handlen = (long)s->ext.ech.innerch_len;
608
0
                hdata = s->ext.ech.innerch;
609
0
            } else
610
0
#endif
611
0
            {
612
0
                handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
613
0
                if (handlen <= 0) {
614
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR,
615
0
                        SSL_R_BAD_HANDSHAKE_LENGTH);
616
0
                    goto err;
617
0
                }
618
0
            }
619
620
            /*
621
             * 0-RTT keys off the recorded first-offered PSK, which may be the
622
             * external psksession rather than s->session.
623
             */
624
0
            if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
625
0
                && s->ext.early_data_session != NULL) {
626
0
                if (!ossl_assert(s->max_early_data
627
0
                        == s->ext.early_data_session->ext.max_early_data)) {
628
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
629
0
                    goto err;
630
0
                }
631
0
                sslcipher = SSL_SESSION_get0_cipher(s->ext.early_data_session);
632
0
            }
633
0
            if (sslcipher == NULL) {
634
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
635
0
                goto err;
636
0
            }
637
638
            /*
639
             * This ups the ref count on cipher so we better make sure we free
640
             * it again
641
             */
642
0
            if (!ssl_cipher_get_evp_cipher(sctx, sslcipher, &cipher)
643
0
                || (SSL_CONNECTION_IS_DTLS(s)
644
0
                    && !ssl_cipher_get_evp_cipher_sn(sctx, sslcipher, &sncipher))) {
645
                /* Error is already recorded */
646
0
                SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
647
0
                goto err;
648
0
            }
649
650
0
            if (((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
651
0
                && (!ssl_cipher_get_evp_md_mac(sctx, sslcipher, &mac_md,
652
0
                    &mac_pkey_type, NULL))) {
653
0
                SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
654
0
                goto err;
655
0
            }
656
657
            /*
658
             * We need to calculate the handshake digest using the digest from
659
             * the session. We haven't yet selected our ciphersuite so we can't
660
             * use ssl_handshake_md().
661
             */
662
0
            mdctx = EVP_MD_CTX_new();
663
0
            if (mdctx == NULL) {
664
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
665
0
                goto err;
666
0
            }
667
668
0
            md = ssl_md(sctx, sslcipher->algorithm2);
669
0
            if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)) {
670
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
671
0
                EVP_MD_CTX_free(mdctx);
672
0
                goto err;
673
0
            }
674
675
0
            if (SSL_CONNECTION_IS_DTLS(s)) {
676
0
                if (!dtls13_transcript_hash_update(mdctx, hdata,
677
0
                        (size_t)handlen)) {
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
            } else {
683
0
                if (!EVP_DigestUpdate(mdctx, hdata, handlen)) {
684
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
685
0
                    EVP_MD_CTX_free(mdctx);
686
0
                    goto err;
687
0
                }
688
0
            }
689
690
0
            if (!EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
691
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
692
0
                EVP_MD_CTX_free(mdctx);
693
0
                goto err;
694
0
            }
695
0
            hashlen = hashlenui;
696
0
            EVP_MD_CTX_free(mdctx);
697
698
0
            if (!tls13_hkdf_expand(s, md, insecret,
699
0
                    early_exporter_master_secret,
700
0
                    sizeof(early_exporter_master_secret) - 1,
701
0
                    hashval, hashlen,
702
0
                    s->early_exporter_master_secret, hashlen,
703
0
                    1)) {
704
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
705
0
                goto err;
706
0
            }
707
708
0
            if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
709
0
                    s->early_exporter_master_secret, hashlen)) {
710
                /* SSLfatal() already called */
711
0
                goto err;
712
0
            }
713
0
            s->ext.early_exporter_ready = 1;
714
0
        } else if (which & SSL3_CC_HANDSHAKE) {
715
0
            insecret = s->handshake_secret;
716
0
            finsecret = s->client_finished_secret;
717
0
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
718
0
            if (finsecretlen <= 0) {
719
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
720
0
                goto err;
721
0
            }
722
0
            label = client_handshake_traffic;
723
0
            labellen = sizeof(client_handshake_traffic) - 1;
724
0
            log_label = CLIENT_HANDSHAKE_LABEL;
725
            /*
726
             * The handshake hash used for the server read/client write handshake
727
             * traffic secret is the same as the hash for the server
728
             * write/client read handshake traffic secret. However, if we
729
             * processed early data then we delay changing the server
730
             * read/client write cipher state until later, and the handshake
731
             * hashes have moved on. Therefore we use the value saved earlier
732
             * when we did the server write/client read change cipher state.
733
             */
734
0
            hash = s->handshake_traffic_hash;
735
0
        } else {
736
0
            insecret = s->master_secret;
737
0
            label = client_application_traffic;
738
0
            labellen = sizeof(client_application_traffic) - 1;
739
0
            log_label = CLIENT_APPLICATION_LABEL;
740
            /*
741
             * For this we only use the handshake hashes up until the server
742
             * Finished hash. We do not include the client's Finished, which is
743
             * what ssl_handshake_hash() would give us. Instead we use the
744
             * previously saved value.
745
             */
746
0
            hash = s->server_finished_hash;
747
0
        }
748
0
    } else {
749
        /* Early data never applies to client-read/server-write */
750
0
        if (which & SSL3_CC_HANDSHAKE) {
751
0
            insecret = s->handshake_secret;
752
0
            finsecret = s->server_finished_secret;
753
0
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
754
0
            if (finsecretlen <= 0) {
755
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
756
0
                goto err;
757
0
            }
758
0
            label = server_handshake_traffic;
759
0
            labellen = sizeof(server_handshake_traffic) - 1;
760
0
            log_label = SERVER_HANDSHAKE_LABEL;
761
0
        } else {
762
0
            insecret = s->master_secret;
763
0
            label = server_application_traffic;
764
0
            labellen = sizeof(server_application_traffic) - 1;
765
0
            log_label = SERVER_APPLICATION_LABEL;
766
0
            hash = s->server_finished_hash;
767
0
        }
768
0
    }
769
770
0
    if ((which & SSL3_CC_EARLY) == 0) {
771
0
        md = ssl_handshake_md(s);
772
0
        cipher = s->s3.tmp.new_sym_enc;
773
0
        mac_md = s->s3.tmp.new_hash;
774
0
        mac_pkey_type = s->s3.tmp.new_mac_pkey_type;
775
776
0
        if (SSL_CONNECTION_IS_DTLS(s)) {
777
0
            sncipher = s->s3.tmp.new_sym_enc_sn;
778
0
        }
779
0
        if (!ssl3_digest_cached_records(s, 1)
780
0
            || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
781
0
            /* SSLfatal() already called */;
782
0
            goto err;
783
0
        }
784
0
    }
785
786
0
    if (label == client_application_traffic) {
787
        /*
788
         * We also create the resumption master secret, but this time use the
789
         * hash for the whole handshake including the Client Finished
790
         */
791
0
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
792
0
                resumption_master_secret,
793
0
                sizeof(resumption_master_secret) - 1,
794
0
                hashval, hashlen, s->resumption_master_secret,
795
0
                hashlen, 1)) {
796
            /* SSLfatal() already called */
797
0
            goto err;
798
0
        }
799
0
    }
800
801
    /* check whether cipher is known */
802
0
    if (!ossl_assert(cipher != NULL))
803
0
        goto err;
804
805
0
    if (!derive_secret_key_and_iv(s, md, cipher, mac_pkey_type, mac_md,
806
0
            insecret, hash, label, labellen, secret,
807
0
            snkey, key, &keylen, &iv, &ivlen, &taglen)) {
808
        /* SSLfatal() already called */
809
0
        goto err;
810
0
    }
811
812
0
    if (label == server_application_traffic) {
813
0
        memcpy(s->server_app_traffic_secret, secret, hashlen);
814
        /* Now we create the exporter master secret */
815
0
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
816
0
                exporter_master_secret,
817
0
                sizeof(exporter_master_secret) - 1,
818
0
                hash, hashlen, s->exporter_master_secret,
819
0
                hashlen, 1)) {
820
            /* SSLfatal() already called */
821
0
            goto err;
822
0
        }
823
824
0
        if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
825
0
                hashlen)) {
826
            /* SSLfatal() already called */
827
0
            goto err;
828
0
        }
829
0
    } else if (label == client_application_traffic)
830
0
        memcpy(s->client_app_traffic_secret, secret, hashlen);
831
832
0
    if (!ssl_log_secret(s, log_label, secret, hashlen)) {
833
        /* SSLfatal() already called */
834
0
        goto err;
835
0
    }
836
837
0
    if (finsecret != NULL
838
0
        && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
839
0
            finsecret, (size_t)finsecretlen)) {
840
        /* SSLfatal() already called */
841
0
        goto err;
842
0
    }
843
844
0
    if ((which & SSL3_CC_WRITE) != 0) {
845
0
        if (!s->server && label == client_early_traffic)
846
0
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 1);
847
0
        else
848
0
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 0);
849
0
    }
850
851
0
    level = (which & SSL3_CC_EARLY) != 0
852
0
        ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
853
0
        : ((which & SSL3_CC_HANDSHAKE) != 0
854
0
                  ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
855
0
                  : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
856
857
0
    if (SSL_CONNECTION_IS_DTLS(s)) {
858
        /*
859
         * For DTLS1.3 The Compressed Certificate should still be sent in Epoch 2
860
         * not Epoch 3.
861
         */
862
0
        if (s->version != DTLS1_3_VERSION || (which & SSL3_CC_COMP_CERT) == 0) {
863
0
            if (!dtls1_increment_epoch(s, which)) {
864
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
865
0
                goto err;
866
0
            }
867
0
        }
868
869
0
        if (level == OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE && dtls1_get_epoch(s, which) == 1) {
870
            /*
871
             * We must manually increment epoch because
872
             * client early traffic was not sent/recv
873
             */
874
0
            if (!dtls1_increment_epoch(s, which)) {
875
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
876
0
                goto err;
877
0
            }
878
0
        }
879
880
        /* We have moved to the next flight lets clear out old messages */
881
0
        if (direction == OSSL_RECORD_DIRECTION_READ)
882
0
            dtls1_clear_received_buffer(s);
883
884
0
        dtls1_clear_sent_buffer(s, 1);
885
0
    }
886
887
0
    if (!ssl_set_new_record_layer(s, s->version, direction, level, secret,
888
0
            hashlen, snkey, key, keylen, iv, ivlen,
889
0
            NULL, 0, sncipher, cipher, taglen,
890
0
            mac_pkey_type, mac_md, NULL, md)) {
891
        /* SSLfatal already called */
892
0
        goto err;
893
0
    }
894
895
0
    ret = 1;
896
0
err:
897
0
    if ((which & SSL3_CC_EARLY) != 0) {
898
        /* We up-refed this so now we need to down ref */
899
0
        if ((EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0)
900
0
            ssl_evp_md_free(mac_md);
901
0
        ssl_evp_cipher_free(cipher);
902
0
        ssl_evp_cipher_free(sncipher);
903
0
    }
904
0
    OPENSSL_cleanse(key, sizeof(key));
905
0
    OPENSSL_cleanse(snkey, sizeof(snkey));
906
0
    OPENSSL_cleanse(secret, sizeof(secret));
907
0
    if (iv != iv_intern)
908
0
        OPENSSL_free(iv);
909
0
    return ret;
910
0
}
911
912
int tls13_update_key(SSL_CONNECTION *s, int sending)
913
0
{
914
    /* ASCII: "traffic upd", in hex for EBCDIC compatibility */
915
0
    static const unsigned char application_traffic[] = "\x74\x72\x61\x66\x66\x69\x63\x20\x75\x70\x64";
916
0
    const EVP_MD *md = ssl_handshake_md(s);
917
0
    size_t hashlen;
918
0
    unsigned char key[EVP_MAX_KEY_LENGTH];
919
0
    unsigned char snkey[EVP_MAX_KEY_LENGTH];
920
0
    const EVP_CIPHER *snenc = NULL;
921
0
    unsigned char *insecret;
922
0
    unsigned char secret[EVP_MAX_MD_SIZE];
923
0
    char *log_label;
924
0
    size_t keylen, ivlen, taglen;
925
0
    int ret = 0, l;
926
0
    int direction = sending ? OSSL_RECORD_DIRECTION_WRITE
927
0
                            : OSSL_RECORD_DIRECTION_READ;
928
0
    int which = sending ? SSL3_CC_WRITE : SSL3_CC_READ;
929
0
    unsigned char iv_intern[EVP_MAX_IV_LENGTH];
930
0
    unsigned char *iv = iv_intern;
931
932
0
    if ((l = EVP_MD_get_size(md)) <= 0) {
933
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
934
0
        return 0;
935
0
    }
936
0
    hashlen = (size_t)l;
937
938
0
    if (s->server == sending)
939
0
        insecret = s->server_app_traffic_secret;
940
0
    else
941
0
        insecret = s->client_app_traffic_secret;
942
943
0
    if (!derive_secret_key_and_iv(s, md,
944
0
            s->s3.tmp.new_sym_enc,
945
0
            s->s3.tmp.new_mac_pkey_type, s->s3.tmp.new_hash,
946
0
            insecret, NULL,
947
0
            application_traffic,
948
0
            sizeof(application_traffic) - 1, secret, snkey,
949
0
            key, &keylen, &iv, &ivlen, &taglen)) {
950
        /* SSLfatal() already called */
951
0
        goto err;
952
0
    }
953
954
0
    memcpy(insecret, secret, hashlen);
955
956
0
    if (SSL_CONNECTION_IS_DTLS(s)) {
957
0
        if (!dtls1_increment_epoch(s, which)) {
958
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
959
0
            goto err;
960
0
        }
961
0
        snenc = s->s3.tmp.new_sym_enc_sn;
962
0
    }
963
964
0
    if (!ssl_set_new_record_layer(s, s->version, direction,
965
0
            OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
966
0
            insecret, hashlen, snkey, key, keylen,
967
0
            iv, ivlen, NULL, 0,
968
0
            snenc,
969
0
            s->s3.tmp.new_sym_enc,
970
0
            taglen, NID_undef, NULL, NULL, md)) {
971
        /* SSLfatal already called */
972
0
        goto err;
973
0
    }
974
975
    /* Call Key log on successful traffic secret update */
976
0
    log_label = s->server == sending ? SERVER_APPLICATION_N_LABEL : CLIENT_APPLICATION_N_LABEL;
977
0
    if (!ssl_log_secret(s, log_label, secret, hashlen)) {
978
        /* SSLfatal() already called */
979
0
        goto err;
980
0
    }
981
0
    ret = 1;
982
0
err:
983
0
    OPENSSL_cleanse(key, sizeof(key));
984
0
    OPENSSL_cleanse(snkey, sizeof(snkey));
985
0
    OPENSSL_cleanse(secret, sizeof(secret));
986
0
    if (iv != iv_intern)
987
0
        OPENSSL_free(iv);
988
0
    return ret;
989
0
}
990
991
int tls13_alert_code(int code)
992
0
{
993
    /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
994
0
    if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
995
0
        return code;
996
997
0
    return tls1_alert_code(code);
998
0
}
999
1000
int tls13_export_keying_material(SSL_CONNECTION *s,
1001
    unsigned char *out, size_t olen,
1002
    const char *label, size_t llen,
1003
    const unsigned char *context,
1004
    size_t contextlen, int use_context)
1005
0
{
1006
0
    unsigned char exportsecret[EVP_MAX_MD_SIZE];
1007
    /* ASCII: "exporter", in hex for EBCDIC compatibility */
1008
0
    static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
1009
0
    unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
1010
0
    const EVP_MD *md = ssl_handshake_md(s);
1011
0
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1012
0
    unsigned int hashsize, datalen;
1013
0
    int ret = 0;
1014
1015
0
    if (ctx == NULL || md == NULL || !ossl_statem_export_allowed(s))
1016
0
        goto err;
1017
1018
0
    if (!use_context)
1019
0
        contextlen = 0;
1020
1021
0
    if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
1022
0
        || EVP_DigestUpdate(ctx, context, contextlen) <= 0
1023
0
        || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
1024
0
        || EVP_DigestInit_ex(ctx, md, NULL) <= 0
1025
0
        || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
1026
0
        || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
1027
0
            (const unsigned char *)label, llen,
1028
0
            data, datalen, exportsecret, hashsize, 0)
1029
0
        || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
1030
0
            sizeof(exporterlabel) - 1, hash, hashsize,
1031
0
            out, olen, 0))
1032
0
        goto err;
1033
1034
0
    ret = 1;
1035
0
err:
1036
0
    EVP_MD_CTX_free(ctx);
1037
0
    return ret;
1038
0
}
1039
1040
int tls13_export_keying_material_early(SSL_CONNECTION *s,
1041
    unsigned char *out, size_t olen,
1042
    const char *label, size_t llen,
1043
    const unsigned char *context,
1044
    size_t contextlen)
1045
0
{
1046
    /* ASCII: "exporter", in hex for EBCDIC compatibility */
1047
0
    static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
1048
0
    unsigned char exportsecret[EVP_MAX_MD_SIZE];
1049
0
    unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
1050
0
    const EVP_MD *md;
1051
0
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
1052
0
    unsigned int hashsize, datalen;
1053
0
    int ret = 0;
1054
0
    const SSL_CIPHER *sslcipher;
1055
1056
0
    if (ctx == NULL || !ossl_statem_export_early_allowed(s))
1057
0
        goto err;
1058
1059
0
    if (!s->server && s->ext.early_data_session != NULL)
1060
0
        sslcipher = SSL_SESSION_get0_cipher(s->ext.early_data_session);
1061
0
    else
1062
0
        sslcipher = SSL_SESSION_get0_cipher(s->session);
1063
0
    if (sslcipher == NULL)
1064
0
        goto err;
1065
1066
0
    md = ssl_md(SSL_CONNECTION_GET_CTX(s), sslcipher->algorithm2);
1067
1068
    /*
1069
     * Calculate the hash value and store it in |data|. The reason why
1070
     * the empty string is used is that the definition of TLS-Exporter
1071
     * is like so:
1072
     *
1073
     * TLS-Exporter(label, context_value, key_length) =
1074
     *     HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
1075
     *                       "exporter", Hash(context_value), key_length)
1076
     *
1077
     * Derive-Secret(Secret, Label, Messages) =
1078
     *       HKDF-Expand-Label(Secret, Label,
1079
     *                         Transcript-Hash(Messages), Hash.length)
1080
     *
1081
     * Here Transcript-Hash is the cipher suite hash algorithm.
1082
     */
1083
0
    if (md == NULL
1084
0
        || EVP_DigestInit_ex(ctx, md, NULL) <= 0
1085
0
        || EVP_DigestUpdate(ctx, context, contextlen) <= 0
1086
0
        || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
1087
0
        || EVP_DigestInit_ex(ctx, md, NULL) <= 0
1088
0
        || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
1089
0
        || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
1090
0
            (const unsigned char *)label, llen,
1091
0
            data, datalen, exportsecret, hashsize, 0)
1092
0
        || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
1093
0
            sizeof(exporterlabel) - 1, hash, hashsize,
1094
0
            out, olen, 0))
1095
0
        goto err;
1096
1097
0
    ret = 1;
1098
0
err:
1099
0
    EVP_MD_CTX_free(ctx);
1100
0
    return ret;
1101
0
}