Coverage Report

Created: 2025-12-31 06:58

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