Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/providers/implementations/kdfs/tls1_prf.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
/*
11
 * Refer to "The TLS Protocol Version 1.0" Section 5
12
 * (https://tools.ietf.org/html/rfc2246#section-5) and
13
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
14
 * (https://tools.ietf.org/html/rfc5246#section-5).
15
 *
16
 * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
17
 *
18
 *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
19
 *                              P_SHA-1(S2, label + seed)
20
 *
21
 * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
22
 * two halves of the secret (with the possibility of one shared byte, in the
23
 * case where the length of the original secret is odd).  S1 is taken from the
24
 * first half of the secret, S2 from the second half.
25
 *
26
 * For TLS v1.2 the TLS PRF algorithm is given by:
27
 *
28
 *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
29
 *
30
 * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
31
 * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
32
 * unless defined otherwise by the cipher suite.
33
 *
34
 * P_<hash> is an expansion function that uses a single hash function to expand
35
 * a secret and seed into an arbitrary quantity of output:
36
 *
37
 *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
38
 *                            HMAC_<hash>(secret, A(2) + seed) +
39
 *                            HMAC_<hash>(secret, A(3) + seed) + ...
40
 *
41
 * where + indicates concatenation.  P_<hash> can be iterated as many times as
42
 * is necessary to produce the required quantity of data.
43
 *
44
 * A(i) is defined as:
45
 *     A(0) = seed
46
 *     A(i) = HMAC_<hash>(secret, A(i-1))
47
 */
48
49
/*
50
 * Low level APIs (such as DH) are deprecated for public use, but still ok for
51
 * internal use.
52
 */
53
#include "internal/deprecated.h"
54
55
#include <stdio.h>
56
#include <stdarg.h>
57
#include <string.h>
58
#include <openssl/evp.h>
59
#include <openssl/kdf.h>
60
#include <openssl/core_names.h>
61
#include <openssl/params.h>
62
#include <openssl/proverr.h>
63
#include "internal/cryptlib.h"
64
#include "internal/numbers.h"
65
#include "crypto/evp.h"
66
#include "prov/provider_ctx.h"
67
#include "prov/providercommon.h"
68
#include "prov/implementations.h"
69
#include "prov/provider_util.h"
70
#include "prov/securitycheck.h"
71
#include "internal/e_os.h"
72
#include "internal/safe_math.h"
73
74
OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
75
76
static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
77
static OSSL_FUNC_kdf_dupctx_fn kdf_tls1_prf_dup;
78
static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
79
static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
80
static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
81
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
82
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
83
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
84
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
85
86
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
87
    const unsigned char *sec, size_t slen,
88
    const unsigned char *seed, size_t seed_len,
89
    unsigned char *out, size_t olen);
90
91
0
#define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"
92
0
#define TLS_MD_MASTER_SECRET_CONST_SIZE 13
93
94
/* TLS KDF kdf context structure */
95
typedef struct {
96
    void *provctx;
97
98
    /* MAC context for the main digest */
99
    EVP_MAC_CTX *P_hash;
100
    /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
101
    EVP_MAC_CTX *P_sha1;
102
103
    /* Secret value to use for PRF */
104
    unsigned char *sec;
105
    size_t seclen;
106
    /* Concatenated seed data */
107
    unsigned char *seed;
108
    size_t seedlen;
109
} TLS1_PRF;
110
111
static void *kdf_tls1_prf_new(void *provctx)
112
65.0k
{
113
65.0k
    TLS1_PRF *ctx;
114
115
65.0k
    if (!ossl_prov_is_running())
116
0
        return NULL;
117
118
65.0k
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL)
119
65.0k
        ctx->provctx = provctx;
120
65.0k
    return ctx;
121
65.0k
}
122
123
static void kdf_tls1_prf_free(void *vctx)
124
70.4k
{
125
70.4k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
126
127
70.4k
    if (ctx != NULL) {
128
70.4k
        kdf_tls1_prf_reset(ctx);
129
70.4k
        OPENSSL_free(ctx);
130
70.4k
    }
131
70.4k
}
132
133
static void kdf_tls1_prf_reset(void *vctx)
134
70.4k
{
135
70.4k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
136
70.4k
    void *provctx = ctx->provctx;
137
138
70.4k
    EVP_MAC_CTX_free(ctx->P_hash);
139
70.4k
    EVP_MAC_CTX_free(ctx->P_sha1);
140
70.4k
    OPENSSL_clear_free(ctx->sec, ctx->seclen);
141
70.4k
    OPENSSL_clear_free(ctx->seed, ctx->seedlen);
142
70.4k
    memset(ctx, 0, sizeof(*ctx));
143
70.4k
    ctx->provctx = provctx;
144
70.4k
}
145
146
static void *kdf_tls1_prf_dup(void *vctx)
147
0
{
148
0
    const TLS1_PRF *src = (const TLS1_PRF *)vctx;
149
0
    TLS1_PRF *dest;
150
151
0
    dest = kdf_tls1_prf_new(src->provctx);
152
0
    if (dest != NULL) {
153
0
        if (src->P_hash != NULL
154
0
            && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
155
0
            goto err;
156
0
        if (src->P_sha1 != NULL
157
0
            && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
158
0
            goto err;
159
0
        if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
160
0
            goto err;
161
0
        if (!ossl_prov_memdup(src->seed, src->seedlen, &dest->seed,
162
0
                &dest->seedlen))
163
0
            goto err;
164
0
    }
165
0
    return dest;
166
167
0
err:
168
0
    kdf_tls1_prf_free(dest);
169
0
    return NULL;
170
0
}
171
172
static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
173
    const OSSL_PARAM params[])
174
13.8k
{
175
13.8k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
176
13.8k
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
177
178
13.8k
    if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
179
0
        return 0;
180
181
13.8k
    if (ctx->P_hash == NULL) {
182
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
183
0
        return 0;
184
0
    }
185
13.8k
    if (ctx->sec == NULL) {
186
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
187
0
        return 0;
188
0
    }
189
13.8k
    if (ctx->seedlen == 0) {
190
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
191
0
        return 0;
192
0
    }
193
13.8k
    if (keylen == 0) {
194
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
195
0
        return 0;
196
0
    }
197
198
    /*
199
     * The seed buffer is prepended with a label.
200
     * If EMS mode is enforced then the label "master secret" is not allowed,
201
     * We do the check this way since the PRF is used for other purposes, as well
202
     * as "extended master secret".
203
     */
204
13.8k
    if (ossl_tls1_prf_ems_check_enabled(libctx)) {
205
0
        if (ctx->seedlen >= TLS_MD_MASTER_SECRET_CONST_SIZE
206
0
            && memcmp(ctx->seed, TLS_MD_MASTER_SECRET_CONST,
207
0
                   TLS_MD_MASTER_SECRET_CONST_SIZE)
208
0
                == 0) {
209
0
            ERR_raise(ERR_LIB_PROV, PROV_R_EMS_NOT_ENABLED);
210
0
            return 0;
211
0
        }
212
0
    }
213
214
13.8k
    return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
215
13.8k
        ctx->sec, ctx->seclen,
216
13.8k
        ctx->seed, ctx->seedlen,
217
13.8k
        key, keylen);
218
13.8k
}
219
220
static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
221
13.8k
{
222
13.8k
    const OSSL_PARAM *p;
223
13.8k
    TLS1_PRF *ctx = vctx;
224
13.8k
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
225
226
13.8k
    if (params == NULL)
227
0
        return 1;
228
229
13.8k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
230
13.8k
        if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
231
5.62k
            if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
232
5.62k
                    OSSL_MAC_NAME_HMAC,
233
5.62k
                    NULL, SN_md5, libctx)
234
5.62k
                || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
235
5.62k
                    OSSL_MAC_NAME_HMAC,
236
5.62k
                    NULL, SN_sha1, libctx))
237
0
                return 0;
238
8.21k
        } else {
239
8.21k
            EVP_MAC_CTX_free(ctx->P_sha1);
240
8.21k
            if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
241
8.21k
                    OSSL_MAC_NAME_HMAC,
242
8.21k
                    NULL, NULL, libctx))
243
0
                return 0;
244
8.21k
        }
245
13.8k
    }
246
247
13.8k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
248
13.8k
        OPENSSL_clear_free(ctx->sec, ctx->seclen);
249
13.8k
        ctx->sec = NULL;
250
13.8k
        if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
251
0
            return 0;
252
13.8k
    }
253
    /* The seed fields concatenate, so process them all */
254
13.8k
    if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
255
83.0k
        for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
256
69.1k
                              OSSL_KDF_PARAM_SEED)) {
257
69.1k
            if (p->data_size != 0 && p->data != NULL) {
258
37.6k
                const void *val = NULL;
259
37.6k
                size_t sz = 0;
260
37.6k
                unsigned char *seed;
261
37.6k
                size_t seedlen;
262
37.6k
                int err = 0;
263
264
37.6k
                if (!OSSL_PARAM_get_octet_string_ptr(p, &val, &sz))
265
0
                    return 0;
266
267
37.6k
                seedlen = safe_add_size_t(ctx->seedlen, sz, &err);
268
37.6k
                if (err)
269
0
                    return 0;
270
271
37.6k
                seed = OPENSSL_clear_realloc(ctx->seed, ctx->seedlen, seedlen);
272
37.6k
                if (!seed)
273
0
                    return 0;
274
275
37.6k
                ctx->seed = seed;
276
37.6k
                if (ossl_assert(sz != 0))
277
37.6k
                    memcpy(ctx->seed + ctx->seedlen, val, sz);
278
37.6k
                ctx->seedlen = seedlen;
279
37.6k
            }
280
69.1k
        }
281
13.8k
    }
282
13.8k
    return 1;
283
13.8k
}
284
285
static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
286
    ossl_unused void *ctx, ossl_unused void *provctx)
287
2.39k
{
288
2.39k
    static const OSSL_PARAM known_settable_ctx_params[] = {
289
2.39k
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
290
2.39k
        OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
291
2.39k
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
292
2.39k
        OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
293
2.39k
        OSSL_PARAM_END
294
2.39k
    };
295
2.39k
    return known_settable_ctx_params;
296
2.39k
}
297
298
static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
299
0
{
300
0
    OSSL_PARAM *p;
301
302
0
    if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
303
0
        return OSSL_PARAM_set_size_t(p, SIZE_MAX);
304
0
    return -2;
305
0
}
306
307
static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
308
    ossl_unused void *ctx, ossl_unused void *provctx)
309
0
{
310
0
    static const OSSL_PARAM known_gettable_ctx_params[] = {
311
0
        OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
312
0
        OSSL_PARAM_END
313
0
    };
314
0
    return known_gettable_ctx_params;
315
0
}
316
317
const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
318
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_tls1_prf_new },
319
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_tls1_prf_dup },
320
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_tls1_prf_free },
321
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_tls1_prf_reset },
322
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_tls1_prf_derive },
323
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
324
        (void (*)(void))kdf_tls1_prf_settable_ctx_params },
325
    { OSSL_FUNC_KDF_SET_CTX_PARAMS,
326
        (void (*)(void))kdf_tls1_prf_set_ctx_params },
327
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
328
        (void (*)(void))kdf_tls1_prf_gettable_ctx_params },
329
    { OSSL_FUNC_KDF_GET_CTX_PARAMS,
330
        (void (*)(void))kdf_tls1_prf_get_ctx_params },
331
    OSSL_DISPATCH_END
332
};
333
334
/*
335
 * Refer to "The TLS Protocol Version 1.0" Section 5
336
 * (https://tools.ietf.org/html/rfc2246#section-5) and
337
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
338
 * (https://tools.ietf.org/html/rfc5246#section-5).
339
 *
340
 * P_<hash> is an expansion function that uses a single hash function to expand
341
 * a secret and seed into an arbitrary quantity of output:
342
 *
343
 *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
344
 *                            HMAC_<hash>(secret, A(2) + seed) +
345
 *                            HMAC_<hash>(secret, A(3) + seed) + ...
346
 *
347
 * where + indicates concatenation.  P_<hash> can be iterated as many times as
348
 * is necessary to produce the required quantity of data.
349
 *
350
 * A(i) is defined as:
351
 *     A(0) = seed
352
 *     A(i) = HMAC_<hash>(secret, A(i-1))
353
 */
354
static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
355
    const unsigned char *sec, size_t sec_len,
356
    const unsigned char *seed, size_t seed_len,
357
    unsigned char *out, size_t olen)
358
90.1k
{
359
90.1k
    size_t chunk;
360
90.1k
    EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
361
90.1k
    unsigned char Ai[EVP_MAX_MD_SIZE];
362
90.1k
    size_t Ai_len;
363
90.1k
    int ret = 0;
364
365
90.1k
    if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
366
0
        goto err;
367
90.1k
    chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
368
90.1k
    if (chunk == 0)
369
10
        goto err;
370
    /* A(0) = seed */
371
90.1k
    ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
372
90.1k
    if (ctx_Ai == NULL)
373
0
        goto err;
374
90.1k
    if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
375
0
        goto err;
376
377
238k
    for (;;) {
378
        /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
379
238k
        if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
380
0
            goto err;
381
238k
        EVP_MAC_CTX_free(ctx_Ai);
382
238k
        ctx_Ai = NULL;
383
384
        /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
385
238k
        ctx = EVP_MAC_CTX_dup(ctx_init);
386
238k
        if (ctx == NULL)
387
0
            goto err;
388
238k
        if (!EVP_MAC_update(ctx, Ai, Ai_len))
389
0
            goto err;
390
        /* save state for calculating next A(i) value */
391
238k
        if (olen > chunk) {
392
147k
            ctx_Ai = EVP_MAC_CTX_dup(ctx);
393
147k
            if (ctx_Ai == NULL)
394
0
                goto err;
395
147k
        }
396
238k
        if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
397
0
            goto err;
398
238k
        if (olen <= chunk) {
399
            /* last chunk - use Ai as temp bounce buffer */
400
90.1k
            if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
401
0
                goto err;
402
90.1k
            memcpy(out, Ai, olen);
403
90.1k
            break;
404
90.1k
        }
405
147k
        if (!EVP_MAC_final(ctx, out, NULL, olen))
406
0
            goto err;
407
147k
        EVP_MAC_CTX_free(ctx);
408
147k
        ctx = NULL;
409
147k
        out += chunk;
410
147k
        olen -= chunk;
411
147k
    }
412
90.1k
    ret = 1;
413
90.1k
err:
414
90.1k
    EVP_MAC_CTX_free(ctx);
415
90.1k
    EVP_MAC_CTX_free(ctx_Ai);
416
90.1k
    OPENSSL_cleanse(Ai, sizeof(Ai));
417
90.1k
    return ret;
418
90.1k
}
419
420
/*
421
 * Refer to "The TLS Protocol Version 1.0" Section 5
422
 * (https://tools.ietf.org/html/rfc2246#section-5) and
423
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
424
 * (https://tools.ietf.org/html/rfc5246#section-5).
425
 *
426
 * For TLS v1.0 and TLS v1.1:
427
 *
428
 *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
429
 *                              P_SHA-1(S2, label + seed)
430
 *
431
 * S1 is taken from the first half of the secret, S2 from the second half.
432
 *
433
 *   L_S = length in bytes of secret;
434
 *   L_S1 = L_S2 = ceil(L_S / 2);
435
 *
436
 * For TLS v1.2:
437
 *
438
 *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
439
 */
440
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
441
    const unsigned char *sec, size_t slen,
442
    const unsigned char *seed, size_t seed_len,
443
    unsigned char *out, size_t olen)
444
68.6k
{
445
68.6k
    if (sha1ctx != NULL) {
446
        /* TLS v1.0 and TLS v1.1 */
447
21.5k
        size_t i;
448
21.5k
        unsigned char *tmp;
449
        /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
450
21.5k
        size_t L_S1 = (slen + 1) / 2;
451
21.5k
        size_t L_S2 = L_S1;
452
453
21.5k
        if (!tls1_prf_P_hash(mdctx, sec, L_S1,
454
21.5k
                seed, seed_len, out, olen))
455
0
            return 0;
456
457
21.5k
        if ((tmp = OPENSSL_malloc(olen)) == NULL)
458
0
            return 0;
459
460
21.5k
        if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
461
21.5k
                seed, seed_len, tmp, olen)) {
462
0
            OPENSSL_clear_free(tmp, olen);
463
0
            return 0;
464
0
        }
465
1.17M
        for (i = 0; i < olen; i++)
466
1.14M
            out[i] ^= tmp[i];
467
21.5k
        OPENSSL_clear_free(tmp, olen);
468
21.5k
        return 1;
469
21.5k
    }
470
471
    /* TLS v1.2 */
472
47.1k
    if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
473
10
        return 0;
474
475
47.0k
    return 1;
476
47.1k
}