Coverage Report

Created: 2026-07-12 07:21

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