Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/ssl/tls13_enc.c
Line
Count
Source (jump to first uncovered line)
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
530k
#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
530k
{
39
530k
    EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_TLS1_3_KDF, propq);
40
530k
    EVP_KDF_CTX *kctx;
41
530k
    OSSL_PARAM params[7], *p = params;
42
530k
    int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
43
530k
    const char *mdname = EVP_MD_get0_name(md);
44
530k
    int ret;
45
530k
    size_t hashlen;
46
47
530k
    kctx = EVP_KDF_CTX_new(kdf);
48
530k
    EVP_KDF_free(kdf);
49
530k
    if (kctx == NULL)
50
0
        return 0;
51
52
530k
    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
530k
    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
530k
    hashlen = (size_t)ret;
71
72
530k
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
73
530k
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
74
530k
                                            (char *)mdname, 0);
75
530k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
76
530k
                                             (unsigned char *)secret, hashlen);
77
530k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
78
530k
                                             (unsigned char *)label_prefix,
79
530k
                                             sizeof(label_prefix) - 1);
80
530k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
81
530k
                                             (unsigned char *)label, labellen);
82
530k
    if (data != NULL)
83
50.4k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_DATA,
84
50.4k
                                                 (unsigned char *)data,
85
50.4k
                                                 datalen);
86
530k
    *p++ = OSSL_PARAM_construct_end();
87
88
530k
    ret = EVP_KDF_derive(kctx, out, outlen, params) <= 0;
89
530k
    EVP_KDF_CTX_free(kctx);
90
91
530k
    if (ret != 0) {
92
0
        if (raise_error)
93
0
            ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
94
0
    }
95
96
530k
    return ret == 0;
97
530k
}
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
148k
{
105
148k
    int ret;
106
148k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
107
108
148k
    ret = tls13_hkdf_expand_ex(sctx->libctx, sctx->propq, md,
109
148k
                               secret, label, labellen, data, datalen,
110
148k
                               out, outlen, !fatal);
111
148k
    if (ret == 0 && fatal)
112
148k
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
113
114
148k
    return ret;
115
148k
}
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
39.6k
{
125
    /* ASCII: "key", in hex for EBCDIC compatibility */
126
39.6k
    static const unsigned char keylabel[] = "\x6B\x65\x79";
127
128
39.6k
    return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
129
39.6k
                             NULL, 0, key, keylen, 1);
130
39.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
39.6k
{
140
    /* ASCII: "iv", in hex for EBCDIC compatibility */
141
39.6k
    static const unsigned char ivlabel[] = "\x69\x76";
142
143
39.6k
    return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
144
39.6k
                             NULL, 0, iv, ivlen, 1);
145
39.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
26.1k
{
151
    /* ASCII: "finished", in hex for EBCDIC compatibility */
152
26.1k
    static const unsigned char finishedlabel[] = "\x66\x69\x6E\x69\x73\x68\x65\x64";
153
154
26.1k
    return tls13_hkdf_expand(s, md, secret, finishedlabel,
155
26.1k
                             sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
156
26.1k
}
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
18.7k
{
169
18.7k
    size_t mdlen;
170
18.7k
    int mdleni;
171
18.7k
    int ret;
172
18.7k
    EVP_KDF *kdf;
173
18.7k
    EVP_KDF_CTX *kctx;
174
18.7k
    OSSL_PARAM params[7], *p = params;
175
18.7k
    int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
176
18.7k
    const char *mdname = EVP_MD_get0_name(md);
177
    /* ASCII: "derived", in hex for EBCDIC compatibility */
178
18.7k
    static const char derived_secret_label[] = "\x64\x65\x72\x69\x76\x65\x64";
179
18.7k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
180
181
18.7k
    kdf = EVP_KDF_fetch(sctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF, sctx->propq);
182
18.7k
    kctx = EVP_KDF_CTX_new(kdf);
183
18.7k
    EVP_KDF_free(kdf);
184
18.7k
    if (kctx == NULL) {
185
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
186
0
        return 0;
187
0
    }
188
189
18.7k
    mdleni = EVP_MD_get_size(md);
190
    /* Ensure cast to size_t is safe */
191
18.7k
    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
18.7k
    mdlen = (size_t)mdleni;
197
198
18.7k
    *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
199
18.7k
    *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
200
18.7k
                                            (char *)mdname, 0);
201
18.7k
    if (insecret != NULL)
202
7.19k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
203
7.19k
                                                 (unsigned char *)insecret,
204
7.19k
                                                 insecretlen);
205
18.7k
    if (prevsecret != NULL)
206
11.5k
        *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
207
11.5k
                                                 (unsigned char *)prevsecret, mdlen);
208
18.7k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
209
18.7k
                                             (unsigned char *)label_prefix,
210
18.7k
                                             sizeof(label_prefix) - 1);
211
18.7k
    *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
212
18.7k
                                             (unsigned char *)derived_secret_label,
213
18.7k
                                             sizeof(derived_secret_label) - 1);
214
18.7k
    *p++ = OSSL_PARAM_construct_end();
215
216
18.7k
    ret = EVP_KDF_derive(kctx, outsecret, mdlen, params) <= 0;
217
218
18.7k
    if (ret != 0)
219
18.7k
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
220
221
18.7k
    EVP_KDF_CTX_free(kctx);
222
18.7k
    return ret == 0;
223
18.7k
}
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
13.2k
{
234
    /* Calls SSLfatal() if required */
235
13.2k
    return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
236
13.2k
                                 insecret, insecretlen,
237
13.2k
                                 (unsigned char *)&s->handshake_secret);
238
13.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
4.35k
{
249
4.35k
    const EVP_MD *md = ssl_handshake_md(s);
250
251
4.35k
    *secret_size = EVP_MD_get_size(md);
252
    /* Calls SSLfatal() if required */
253
4.35k
    return tls13_generate_secret(s, md, prev, NULL, 0, out);
254
4.35k
}
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
13.5k
{
263
13.5k
    const EVP_MD *md = ssl_handshake_md(s);
264
13.5k
    const char *mdname = EVP_MD_get0_name(md);
265
13.5k
    unsigned char hash[EVP_MAX_MD_SIZE];
266
13.5k
    unsigned char finsecret[EVP_MAX_MD_SIZE];
267
13.5k
    unsigned char *key = NULL;
268
13.5k
    size_t len = 0, hashlen;
269
13.5k
    OSSL_PARAM params[2], *p = params;
270
13.5k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
271
272
13.5k
    if (md == NULL)
273
0
        return 0;
274
275
    /* Safe to cast away const here since we're not "getting" any data */
276
13.5k
    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
13.5k
    *p = OSSL_PARAM_construct_end();
281
282
13.5k
    if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
283
        /* SSLfatal() already called */
284
0
        goto err;
285
0
    }
286
287
13.5k
    if (str == SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->server_finished_label) {
288
7.76k
        key = s->server_finished_secret;
289
7.76k
    } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
290
5.77k
        key = s->client_finished_secret;
291
5.77k
    } 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
13.5k
    if (!EVP_Q_mac(sctx->libctx, "HMAC", sctx->propq, mdname,
300
13.5k
                   params, key, hashlen, hash, hashlen,
301
                   /* outsize as per sizeof(peer_finish_md) */
302
13.5k
                   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
13.5k
 err:
308
13.5k
    OPENSSL_cleanse(finsecret, sizeof(finsecret));
309
13.5k
    return len;
310
13.5k
}
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
13.2k
{
318
13.2k
    const EVP_CIPHER *c;
319
13.2k
    const EVP_MD *hash;
320
321
13.2k
    s->session->cipher = s->s3.tmp.new_cipher;
322
13.2k
    if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, &c, &hash,
323
13.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
13.2k
    ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
330
13.2k
    s->s3.tmp.new_sym_enc = c;
331
13.2k
    ssl_evp_md_free(s->s3.tmp.new_hash);
332
13.2k
    s->s3.tmp.new_hash = hash;
333
334
13.2k
    return 1;
335
13.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
18.4k
{
347
18.4k
    int hashleni = EVP_MD_get_size(md);
348
18.4k
    size_t hashlen;
349
18.4k
    int mode;
350
351
    /* Ensure cast to size_t is safe */
352
18.4k
    if (!ossl_assert(hashleni >= 0)) {
353
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
354
0
        return 0;
355
0
    }
356
18.4k
    hashlen = (size_t)hashleni;
357
358
18.4k
    if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
359
18.4k
                           secret, hashlen, 1)) {
360
        /* SSLfatal() already called */
361
0
        return 0;
362
0
    }
363
364
18.4k
    *keylen = EVP_CIPHER_get_key_length(ciph);
365
366
18.4k
    mode = EVP_CIPHER_get_mode(ciph);
367
18.4k
    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
18.4k
    } else {
388
18.4k
        int iivlen;
389
390
18.4k
        if (mode == EVP_CIPH_GCM_MODE) {
391
17.0k
            *taglen = EVP_GCM_TLS_TAG_LEN;
392
17.0k
        } else {
393
            /* CHACHA20P-POLY1305 */
394
1.43k
            *taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
395
1.43k
        }
396
18.4k
        iivlen = EVP_CIPHER_get_iv_length(ciph);
397
18.4k
        if (iivlen < 0) {
398
0
            SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
399
0
            return 0;
400
0
        }
401
18.4k
        *ivlen = iivlen;
402
18.4k
    }
403
404
18.4k
    if (!tls13_derive_key(s, md, secret, key, *keylen)
405
18.4k
            || !tls13_derive_iv(s, md, secret, iv, *ivlen)) {
406
        /* SSLfatal() already called */
407
0
        return 0;
408
0
    }
409
410
18.4k
    return 1;
411
18.4k
}
412
413
int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
414
18.4k
{
415
    /* ASCII: "c e traffic", in hex for EBCDIC compatibility */
416
18.4k
    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
18.4k
    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
18.4k
    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
18.4k
    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
18.4k
    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
18.4k
    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
18.4k
    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
18.4k
    static const unsigned char early_exporter_master_secret[] = "\x65\x20\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
431
18.4k
    unsigned char iv[EVP_MAX_IV_LENGTH];
432
18.4k
    unsigned char key[EVP_MAX_KEY_LENGTH];
433
18.4k
    unsigned char secret[EVP_MAX_MD_SIZE];
434
18.4k
    unsigned char hashval[EVP_MAX_MD_SIZE];
435
18.4k
    unsigned char *hash = hashval;
436
18.4k
    unsigned char *insecret;
437
18.4k
    unsigned char *finsecret = NULL;
438
18.4k
    const char *log_label = NULL;
439
18.4k
    size_t finsecretlen = 0;
440
18.4k
    const unsigned char *label;
441
18.4k
    size_t labellen, hashlen = 0;
442
18.4k
    int ret = 0;
443
18.4k
    const EVP_MD *md = NULL;
444
18.4k
    const EVP_CIPHER *cipher = NULL;
445
18.4k
    SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
446
18.4k
    size_t keylen, ivlen, taglen;
447
18.4k
    int level;
448
18.4k
    int direction = (which & SSL3_CC_READ) != 0 ? OSSL_RECORD_DIRECTION_READ
449
18.4k
                                                : OSSL_RECORD_DIRECTION_WRITE;
450
451
18.4k
    if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
452
18.4k
            || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
453
8.94k
        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 ==
481
0
                           s->psksession->ext.max_early_data)) {
482
0
                    SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
483
0
                    goto err;
484
0
                }
485
0
                sslcipher = SSL_SESSION_get0_cipher(s->psksession);
486
0
            }
487
0
            if (sslcipher == NULL) {
488
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
489
0
                goto err;
490
0
            }
491
492
            /*
493
             * We need to calculate the handshake digest using the digest from
494
             * the session. We haven't yet selected our ciphersuite so we can't
495
             * use ssl_handshake_md().
496
             */
497
0
            mdctx = EVP_MD_CTX_new();
498
0
            if (mdctx == NULL) {
499
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
500
0
                goto err;
501
0
            }
502
503
            /*
504
             * This ups the ref count on cipher so we better make sure we free
505
             * it again
506
             */
507
0
            if (!ssl_cipher_get_evp_cipher(sctx, sslcipher, &cipher)) {
508
                /* Error is already recorded */
509
0
                SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
510
0
                EVP_MD_CTX_free(mdctx);
511
0
                goto err;
512
0
            }
513
514
0
            md = ssl_md(sctx, sslcipher->algorithm2);
515
0
            if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
516
0
                    || !EVP_DigestUpdate(mdctx, hdata, handlen)
517
0
                    || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
518
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
519
0
                EVP_MD_CTX_free(mdctx);
520
0
                goto err;
521
0
            }
522
0
            hashlen = hashlenui;
523
0
            EVP_MD_CTX_free(mdctx);
524
525
0
            if (!tls13_hkdf_expand(s, md, insecret,
526
0
                                   early_exporter_master_secret,
527
0
                                   sizeof(early_exporter_master_secret) - 1,
528
0
                                   hashval, hashlen,
529
0
                                   s->early_exporter_master_secret, hashlen,
530
0
                                   1)) {
531
0
                SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
532
0
                goto err;
533
0
            }
534
535
0
            if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
536
0
                                s->early_exporter_master_secret, hashlen)) {
537
                /* SSLfatal() already called */
538
0
                goto err;
539
0
            }
540
8.94k
        } else if (which & SSL3_CC_HANDSHAKE) {
541
5.93k
            insecret = s->handshake_secret;
542
5.93k
            finsecret = s->client_finished_secret;
543
5.93k
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
544
5.93k
            label = client_handshake_traffic;
545
5.93k
            labellen = sizeof(client_handshake_traffic) - 1;
546
5.93k
            log_label = CLIENT_HANDSHAKE_LABEL;
547
            /*
548
             * The handshake hash used for the server read/client write handshake
549
             * traffic secret is the same as the hash for the server
550
             * write/client read handshake traffic secret. However, if we
551
             * processed early data then we delay changing the server
552
             * read/client write cipher state until later, and the handshake
553
             * hashes have moved on. Therefore we use the value saved earlier
554
             * when we did the server write/client read change cipher state.
555
             */
556
5.93k
            hash = s->handshake_traffic_hash;
557
5.93k
        } else {
558
3.00k
            insecret = s->master_secret;
559
3.00k
            label = client_application_traffic;
560
3.00k
            labellen = sizeof(client_application_traffic) - 1;
561
3.00k
            log_label = CLIENT_APPLICATION_LABEL;
562
            /*
563
             * For this we only use the handshake hashes up until the server
564
             * Finished hash. We do not include the client's Finished, which is
565
             * what ssl_handshake_hash() would give us. Instead we use the
566
             * previously saved value.
567
             */
568
3.00k
            hash = s->server_finished_hash;
569
3.00k
        }
570
9.52k
    } else {
571
        /* Early data never applies to client-read/server-write */
572
9.52k
        if (which & SSL3_CC_HANDSHAKE) {
573
6.06k
            insecret = s->handshake_secret;
574
6.06k
            finsecret = s->server_finished_secret;
575
6.06k
            finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
576
6.06k
            label = server_handshake_traffic;
577
6.06k
            labellen = sizeof(server_handshake_traffic) - 1;
578
6.06k
            log_label = SERVER_HANDSHAKE_LABEL;
579
6.06k
        } else {
580
3.45k
            insecret = s->master_secret;
581
3.45k
            label = server_application_traffic;
582
3.45k
            labellen = sizeof(server_application_traffic) - 1;
583
3.45k
            log_label = SERVER_APPLICATION_LABEL;
584
3.45k
        }
585
9.52k
    }
586
587
18.4k
    if (!(which & SSL3_CC_EARLY)) {
588
18.4k
        md = ssl_handshake_md(s);
589
18.4k
        cipher = s->s3.tmp.new_sym_enc;
590
18.4k
        if (!ssl3_digest_cached_records(s, 1)
591
18.4k
                || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
592
0
            /* SSLfatal() already called */;
593
0
            goto err;
594
0
        }
595
18.4k
    }
596
597
    /*
598
     * Save the hash of handshakes up to now for use when we calculate the
599
     * client application traffic secret
600
     */
601
18.4k
    if (label == server_application_traffic)
602
3.45k
        memcpy(s->server_finished_hash, hashval, hashlen);
603
604
18.4k
    if (label == server_handshake_traffic)
605
6.06k
        memcpy(s->handshake_traffic_hash, hashval, hashlen);
606
607
18.4k
    if (label == client_application_traffic) {
608
        /*
609
         * We also create the resumption master secret, but this time use the
610
         * hash for the whole handshake including the Client Finished
611
         */
612
3.00k
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
613
3.00k
                               resumption_master_secret,
614
3.00k
                               sizeof(resumption_master_secret) - 1,
615
3.00k
                               hashval, hashlen, s->resumption_master_secret,
616
3.00k
                               hashlen, 1)) {
617
            /* SSLfatal() already called */
618
0
            goto err;
619
0
        }
620
3.00k
    }
621
622
    /* check whether cipher is known */
623
18.4k
    if (!ossl_assert(cipher != NULL))
624
0
        goto err;
625
626
18.4k
    if (!derive_secret_key_and_iv(s, md, cipher,
627
18.4k
                                  insecret, hash, label, labellen, secret, key,
628
18.4k
                                  &keylen, iv, &ivlen, &taglen)) {
629
        /* SSLfatal() already called */
630
0
        goto err;
631
0
    }
632
633
18.4k
    if (label == server_application_traffic) {
634
3.45k
        memcpy(s->server_app_traffic_secret, secret, hashlen);
635
        /* Now we create the exporter master secret */
636
3.45k
        if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
637
3.45k
                               exporter_master_secret,
638
3.45k
                               sizeof(exporter_master_secret) - 1,
639
3.45k
                               hash, hashlen, s->exporter_master_secret,
640
3.45k
                               hashlen, 1)) {
641
            /* SSLfatal() already called */
642
0
            goto err;
643
0
        }
644
645
3.45k
        if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
646
3.45k
                            hashlen)) {
647
            /* SSLfatal() already called */
648
0
            goto err;
649
0
        }
650
15.0k
    } else if (label == client_application_traffic)
651
3.00k
        memcpy(s->client_app_traffic_secret, secret, hashlen);
652
653
18.4k
    if (!ssl_log_secret(s, log_label, secret, hashlen)) {
654
        /* SSLfatal() already called */
655
0
        goto err;
656
0
    }
657
658
18.4k
    if (finsecret != NULL
659
18.4k
            && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
660
12.0k
                                         finsecret, finsecretlen)) {
661
        /* SSLfatal() already called */
662
0
        goto err;
663
0
    }
664
665
18.4k
    if ((which & SSL3_CC_WRITE) != 0) {
666
9.39k
        if (!s->server && label == client_early_traffic)
667
0
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 1);
668
9.39k
        else
669
9.39k
            s->rlayer.wrlmethod->set_plain_alerts(s->rlayer.wrl, 0);
670
9.39k
    }
671
672
18.4k
    level = (which & SSL3_CC_EARLY) != 0
673
18.4k
            ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
674
18.4k
            : ((which &SSL3_CC_HANDSHAKE) != 0
675
18.4k
               ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
676
18.4k
               : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
677
678
18.4k
    if (!ssl_set_new_record_layer(s, s->version,
679
18.4k
                                  direction,
680
18.4k
                                  level, secret, hashlen, key, keylen, iv,
681
18.4k
                                  ivlen, NULL, 0, cipher, taglen, NID_undef,
682
18.4k
                                  NULL, NULL, md)) {
683
        /* SSLfatal already called */
684
8
        goto err;
685
8
    }
686
687
18.4k
    ret = 1;
688
18.4k
 err:
689
18.4k
    if ((which & SSL3_CC_EARLY) != 0) {
690
        /* We up-refed this so now we need to down ref */
691
0
        ssl_evp_cipher_free(cipher);
692
0
    }
693
18.4k
    OPENSSL_cleanse(key, sizeof(key));
694
18.4k
    OPENSSL_cleanse(secret, sizeof(secret));
695
18.4k
    return ret;
696
18.4k
}
697
698
int tls13_update_key(SSL_CONNECTION *s, int sending)
699
0
{
700
    /* ASCII: "traffic upd", in hex for EBCDIC compatibility */
701
0
    static const unsigned char application_traffic[] = "\x74\x72\x61\x66\x66\x69\x63\x20\x75\x70\x64";
702
0
    const EVP_MD *md = ssl_handshake_md(s);
703
0
    size_t hashlen;
704
0
    unsigned char key[EVP_MAX_KEY_LENGTH];
705
0
    unsigned char *insecret;
706
0
    unsigned char secret[EVP_MAX_MD_SIZE];
707
0
    char *log_label;
708
0
    size_t keylen, ivlen, taglen;
709
0
    int ret = 0, l;
710
0
    int direction = sending ? OSSL_RECORD_DIRECTION_WRITE
711
0
                            : OSSL_RECORD_DIRECTION_READ;
712
0
    unsigned char iv[EVP_MAX_IV_LENGTH];
713
714
0
    if ((l = EVP_MD_get_size(md)) <= 0) {
715
0
        SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
716
0
        return 0;
717
0
    }
718
0
    hashlen = (size_t)l;
719
720
0
    if (s->server == sending)
721
0
        insecret = s->server_app_traffic_secret;
722
0
    else
723
0
        insecret = s->client_app_traffic_secret;
724
725
0
    if (!derive_secret_key_and_iv(s, md,
726
0
                                  s->s3.tmp.new_sym_enc, insecret, NULL,
727
0
                                  application_traffic,
728
0
                                  sizeof(application_traffic) - 1, secret, key,
729
0
                                  &keylen, iv, &ivlen, &taglen)) {
730
        /* SSLfatal() already called */
731
0
        goto err;
732
0
    }
733
734
0
    memcpy(insecret, secret, hashlen);
735
736
0
    if (!ssl_set_new_record_layer(s, s->version,
737
0
                            direction,
738
0
                            OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
739
0
                            insecret, hashlen, key, keylen, iv, ivlen, NULL, 0,
740
0
                            s->s3.tmp.new_sym_enc, taglen, NID_undef, NULL,
741
0
                            NULL, md)) {
742
        /* SSLfatal already called */
743
0
        goto err;
744
0
    }
745
746
    /* Call Key log on successful traffic secret update */
747
0
    log_label = s->server == sending ? SERVER_APPLICATION_N_LABEL : CLIENT_APPLICATION_N_LABEL;
748
0
    if (!ssl_log_secret(s, log_label, secret, hashlen)) {
749
        /* SSLfatal() already called */
750
0
        goto err;
751
0
    }
752
0
    ret = 1;
753
0
 err:
754
0
    OPENSSL_cleanse(key, sizeof(key));
755
0
    OPENSSL_cleanse(secret, sizeof(secret));
756
0
    return ret;
757
0
}
758
759
int tls13_alert_code(int code)
760
7.51k
{
761
    /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
762
7.51k
    if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
763
121
        return code;
764
765
7.39k
    return tls1_alert_code(code);
766
7.51k
}
767
768
int tls13_export_keying_material(SSL_CONNECTION *s,
769
                                 unsigned char *out, size_t olen,
770
                                 const char *label, size_t llen,
771
                                 const unsigned char *context,
772
                                 size_t contextlen, int use_context)
773
0
{
774
0
    unsigned char exportsecret[EVP_MAX_MD_SIZE];
775
    /* ASCII: "exporter", in hex for EBCDIC compatibility */
776
0
    static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
777
0
    unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
778
0
    const EVP_MD *md = ssl_handshake_md(s);
779
0
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
780
0
    unsigned int hashsize, datalen;
781
0
    int ret = 0;
782
783
0
    if (ctx == NULL || md == NULL || !ossl_statem_export_allowed(s))
784
0
        goto err;
785
786
0
    if (!use_context)
787
0
        contextlen = 0;
788
789
0
    if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
790
0
            || EVP_DigestUpdate(ctx, context, contextlen) <= 0
791
0
            || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
792
0
            || EVP_DigestInit_ex(ctx, md, NULL) <= 0
793
0
            || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
794
0
            || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
795
0
                                  (const unsigned char *)label, llen,
796
0
                                  data, datalen, exportsecret, hashsize, 0)
797
0
            || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
798
0
                                  sizeof(exporterlabel) - 1, hash, hashsize,
799
0
                                  out, olen, 0))
800
0
        goto err;
801
802
0
    ret = 1;
803
0
 err:
804
0
    EVP_MD_CTX_free(ctx);
805
0
    return ret;
806
0
}
807
808
int tls13_export_keying_material_early(SSL_CONNECTION *s,
809
                                       unsigned char *out, size_t olen,
810
                                       const char *label, size_t llen,
811
                                       const unsigned char *context,
812
                                       size_t contextlen)
813
0
{
814
    /* ASCII: "exporter", in hex for EBCDIC compatibility */
815
0
    static const unsigned char exporterlabel[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
816
0
    unsigned char exportsecret[EVP_MAX_MD_SIZE];
817
0
    unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
818
0
    const EVP_MD *md;
819
0
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
820
0
    unsigned int hashsize, datalen;
821
0
    int ret = 0;
822
0
    const SSL_CIPHER *sslcipher;
823
824
0
    if (ctx == NULL || !ossl_statem_export_early_allowed(s))
825
0
        goto err;
826
827
0
    if (!s->server && s->max_early_data > 0
828
0
            && s->session->ext.max_early_data == 0)
829
0
        sslcipher = SSL_SESSION_get0_cipher(s->psksession);
830
0
    else
831
0
        sslcipher = SSL_SESSION_get0_cipher(s->session);
832
833
0
    md = ssl_md(SSL_CONNECTION_GET_CTX(s), sslcipher->algorithm2);
834
835
    /*
836
     * Calculate the hash value and store it in |data|. The reason why
837
     * the empty string is used is that the definition of TLS-Exporter
838
     * is like so:
839
     *
840
     * TLS-Exporter(label, context_value, key_length) =
841
     *     HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
842
     *                       "exporter", Hash(context_value), key_length)
843
     *
844
     * Derive-Secret(Secret, Label, Messages) =
845
     *       HKDF-Expand-Label(Secret, Label,
846
     *                         Transcript-Hash(Messages), Hash.length)
847
     *
848
     * Here Transcript-Hash is the cipher suite hash algorithm.
849
     */
850
0
    if (md == NULL
851
0
            || EVP_DigestInit_ex(ctx, md, NULL) <= 0
852
0
            || EVP_DigestUpdate(ctx, context, contextlen) <= 0
853
0
            || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
854
0
            || EVP_DigestInit_ex(ctx, md, NULL) <= 0
855
0
            || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
856
0
            || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
857
0
                                  (const unsigned char *)label, llen,
858
0
                                  data, datalen, exportsecret, hashsize, 0)
859
0
            || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
860
0
                                  sizeof(exporterlabel) - 1, hash, hashsize,
861
0
                                  out, olen, 0))
862
0
        goto err;
863
864
0
    ret = 1;
865
0
 err:
866
0
    EVP_MD_CTX_free(ctx);
867
0
    return ret;
868
0
}