Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl36/providers/implementations/kdfs/tls1_prf.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2025 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
/* clang-format off */
10
11
/* clang-format on */
12
13
/*
14
 * Refer to "The TLS Protocol Version 1.0" Section 5
15
 * (https://tools.ietf.org/html/rfc2246#section-5) and
16
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
17
 * (https://tools.ietf.org/html/rfc5246#section-5).
18
 *
19
 * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
20
 *
21
 *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
22
 *                              P_SHA-1(S2, label + seed)
23
 *
24
 * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
25
 * two halves of the secret (with the possibility of one shared byte, in the
26
 * case where the length of the original secret is odd).  S1 is taken from the
27
 * first half of the secret, S2 from the second half.
28
 *
29
 * For TLS v1.2 the TLS PRF algorithm is given by:
30
 *
31
 *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
32
 *
33
 * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
34
 * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
35
 * unless defined otherwise by the cipher suite.
36
 *
37
 * P_<hash> is an expansion function that uses a single hash function to expand
38
 * a secret and seed into an arbitrary quantity of output:
39
 *
40
 *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
41
 *                            HMAC_<hash>(secret, A(2) + seed) +
42
 *                            HMAC_<hash>(secret, A(3) + seed) + ...
43
 *
44
 * where + indicates concatenation.  P_<hash> can be iterated as many times as
45
 * is necessary to produce the required quantity of data.
46
 *
47
 * A(i) is defined as:
48
 *     A(0) = seed
49
 *     A(i) = HMAC_<hash>(secret, A(i-1))
50
 */
51
52
/*
53
 * Low level APIs (such as DH) are deprecated for public use, but still ok for
54
 * internal use.
55
 */
56
#include "internal/deprecated.h"
57
58
#include <stdio.h>
59
#include <stdarg.h>
60
#include <string.h>
61
#include <openssl/evp.h>
62
#include <openssl/kdf.h>
63
#include <openssl/core_names.h>
64
#include <openssl/params.h>
65
#include <openssl/proverr.h>
66
#include "internal/cryptlib.h"
67
#include "internal/numbers.h"
68
#include "crypto/evp.h"
69
#include "prov/provider_ctx.h"
70
#include "prov/providercommon.h"
71
#include "prov/implementations.h"
72
#include "prov/provider_util.h"
73
#include "prov/securitycheck.h"
74
#include "internal/e_os.h"
75
#include "internal/params.h"
76
#include "internal/safe_math.h"
77
78
OSSL_SAFE_MATH_UNSIGNED(size_t, size_t)
79
80
static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
81
static OSSL_FUNC_kdf_dupctx_fn kdf_tls1_prf_dup;
82
static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
83
static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
84
static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
85
static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
86
static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
87
static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
88
static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
89
90
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
91
    const unsigned char *sec, size_t slen,
92
    const unsigned char *seed, size_t seed_len,
93
    unsigned char *out, size_t olen);
94
95
#define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"
96
#define TLS_MD_MASTER_SECRET_CONST_SIZE 13
97
98
0
#define TLSPRF_MAX_SEEDS 6
99
100
/* TLS KDF kdf context structure */
101
typedef struct {
102
    void *provctx;
103
104
    /* MAC context for the main digest */
105
    EVP_MAC_CTX *P_hash;
106
    /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
107
    EVP_MAC_CTX *P_sha1;
108
109
    /* Secret value to use for PRF */
110
    unsigned char *sec;
111
    size_t seclen;
112
    /* Concatenated seed data */
113
    unsigned char *seed;
114
    size_t seedlen;
115
116
    OSSL_FIPS_IND_DECLARE
117
} TLS1_PRF;
118
119
static void *kdf_tls1_prf_new(void *provctx)
120
65.0k
{
121
65.0k
    TLS1_PRF *ctx;
122
123
65.0k
    if (!ossl_prov_is_running())
124
0
        return NULL;
125
126
65.0k
    if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) {
127
65.0k
        ctx->provctx = provctx;
128
65.0k
        OSSL_FIPS_IND_INIT(ctx)
129
65.0k
    }
130
65.0k
    return ctx;
131
65.0k
}
132
133
static void kdf_tls1_prf_free(void *vctx)
134
70.4k
{
135
70.4k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
136
137
70.4k
    if (ctx != NULL) {
138
70.4k
        kdf_tls1_prf_reset(ctx);
139
70.4k
        OPENSSL_free(ctx);
140
70.4k
    }
141
70.4k
}
142
143
static void kdf_tls1_prf_reset(void *vctx)
144
70.4k
{
145
70.4k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
146
70.4k
    void *provctx = ctx->provctx;
147
148
70.4k
    EVP_MAC_CTX_free(ctx->P_hash);
149
70.4k
    EVP_MAC_CTX_free(ctx->P_sha1);
150
70.4k
    OPENSSL_clear_free(ctx->sec, ctx->seclen);
151
70.4k
    OPENSSL_clear_free(ctx->seed, ctx->seedlen);
152
70.4k
    memset(ctx, 0, sizeof(*ctx));
153
70.4k
    ctx->provctx = provctx;
154
70.4k
}
155
156
static void *kdf_tls1_prf_dup(void *vctx)
157
0
{
158
0
    const TLS1_PRF *src = (const TLS1_PRF *)vctx;
159
0
    TLS1_PRF *dest;
160
161
0
    dest = kdf_tls1_prf_new(src->provctx);
162
0
    if (dest != NULL) {
163
0
        if (src->P_hash != NULL
164
0
            && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
165
0
            goto err;
166
0
        if (src->P_sha1 != NULL
167
0
            && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
168
0
            goto err;
169
0
        if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
170
0
            goto err;
171
0
        if (!ossl_prov_memdup(src->seed, src->seedlen, &dest->seed,
172
0
                &dest->seedlen))
173
0
            goto err;
174
0
        OSSL_FIPS_IND_COPY(dest, src)
175
0
    }
176
0
    return dest;
177
178
0
err:
179
0
    kdf_tls1_prf_free(dest);
180
0
    return NULL;
181
0
}
182
183
#ifdef FIPS_MODULE
184
185
static int fips_ems_check_passed(TLS1_PRF *ctx)
186
{
187
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
188
    /*
189
     * Check that TLS is using EMS.
190
     *
191
     * The seed buffer is prepended with a label.
192
     * If EMS mode is enforced then the label "master secret" is not allowed,
193
     * We do the check this way since the PRF is used for other purposes, as well
194
     * as "extended master secret".
195
     */
196
    int ems_approved = (ctx->seedlen < TLS_MD_MASTER_SECRET_CONST_SIZE
197
        || memcmp(ctx->seed, TLS_MD_MASTER_SECRET_CONST,
198
               TLS_MD_MASTER_SECRET_CONST_SIZE)
199
            != 0);
200
201
    if (!ems_approved) {
202
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,
203
                libctx, "TLS_PRF", "EMS",
204
                ossl_fips_config_tls1_prf_ems_check)) {
205
            ERR_raise(ERR_LIB_PROV, PROV_R_EMS_NOT_ENABLED);
206
            return 0;
207
        }
208
    }
209
    return 1;
210
}
211
212
static int fips_digest_check_passed(TLS1_PRF *ctx, const EVP_MD *md)
213
{
214
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
215
    /*
216
     * Perform digest check
217
     *
218
     * According to NIST SP 800-135r1 section 5.2, the valid hash functions are
219
     * specified in FIPS 180-3. ACVP also only lists the same set of hash
220
     * functions.
221
     */
222
    int digest_unapproved = !EVP_MD_is_a(md, SN_sha256)
223
        && !EVP_MD_is_a(md, SN_sha384)
224
        && !EVP_MD_is_a(md, SN_sha512);
225
226
    if (digest_unapproved) {
227
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1,
228
                libctx, "TLS_PRF", "Digest",
229
                ossl_fips_config_tls1_prf_digest_check)) {
230
            ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);
231
            return 0;
232
        }
233
    }
234
    return 1;
235
}
236
237
static int fips_key_check_passed(TLS1_PRF *ctx)
238
{
239
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
240
    int key_approved = ossl_kdf_check_key_size(ctx->seclen);
241
242
    if (!key_approved) {
243
        if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,
244
                libctx, "TLS_PRF", "Key size",
245
                ossl_fips_config_tls1_prf_key_check)) {
246
            ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
247
            return 0;
248
        }
249
    }
250
    return 1;
251
}
252
#endif
253
254
static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
255
    const OSSL_PARAM params[])
256
55.3k
{
257
55.3k
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
258
259
55.3k
    if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
260
0
        return 0;
261
262
55.3k
    if (ctx->P_hash == NULL) {
263
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
264
0
        return 0;
265
0
    }
266
55.3k
    if (ctx->sec == NULL) {
267
0
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
268
0
        return 0;
269
0
    }
270
55.3k
    if (ctx->seedlen == 0) {
271
512
        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
272
512
        return 0;
273
512
    }
274
54.8k
    if (keylen == 0) {
275
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
276
0
        return 0;
277
0
    }
278
279
#ifdef FIPS_MODULE
280
    if (!fips_ems_check_passed(ctx))
281
        return 0;
282
#endif
283
284
54.8k
    return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
285
54.8k
        ctx->sec, ctx->seclen,
286
54.8k
        ctx->seed, ctx->seedlen,
287
54.8k
        key, keylen);
288
54.8k
}
289
290
/* clang-format off */
291
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
292
#ifndef tls1prf_set_ctx_params_list
293
static const OSSL_PARAM tls1prf_set_ctx_params_list[] = {
294
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
295
    OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
296
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
297
    OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
298
# if defined(FIPS_MODULE)
299
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_EMS_CHECK, NULL),
300
# endif
301
# if defined(FIPS_MODULE)
302
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK, NULL),
303
# endif
304
# if defined(FIPS_MODULE)
305
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_KEY_CHECK, NULL),
306
# endif
307
    OSSL_PARAM_END
308
};
309
#endif
310
311
#ifndef tls1prf_set_ctx_params_st
312
struct tls1prf_set_ctx_params_st {
313
    OSSL_PARAM *digest;
314
    OSSL_PARAM *engine;
315
# if defined(FIPS_MODULE)
316
    OSSL_PARAM *ind_d;
317
# endif
318
# if defined(FIPS_MODULE)
319
    OSSL_PARAM *ind_e;
320
# endif
321
# if defined(FIPS_MODULE)
322
    OSSL_PARAM *ind_k;
323
# endif
324
    OSSL_PARAM *propq;
325
    OSSL_PARAM *secret;
326
    OSSL_PARAM *seed[TLSPRF_MAX_SEEDS];
327
    int num_seed;
328
};
329
#endif
330
331
#ifndef tls1prf_set_ctx_params_decoder
332
static int tls1prf_set_ctx_params_decoder
333
    (const OSSL_PARAM *p, struct tls1prf_set_ctx_params_st *r)
334
10.6k
{
335
10.6k
    const char *s;
336
337
10.6k
    memset(r, 0, sizeof(*r));
338
10.6k
    if (p != NULL)
339
82.0k
        for (; (s = p->key) != NULL; p++)
340
71.5k
            switch(s[0]) {
341
0
            default:
342
0
                break;
343
10.4k
            case 'd':
344
10.4k
                switch(s[1]) {
345
0
                default:
346
0
                    break;
347
10.4k
                case 'i':
348
10.4k
                    switch(s[2]) {
349
0
                    default:
350
0
                        break;
351
10.4k
                    case 'g':
352
10.4k
                        switch(s[3]) {
353
0
                        default:
354
0
                            break;
355
10.4k
                        case 'e':
356
10.4k
                            switch(s[4]) {
357
0
                            default:
358
0
                                break;
359
10.4k
                            case 's':
360
10.4k
                                switch(s[5]) {
361
0
                                default:
362
0
                                    break;
363
10.4k
                                case 't':
364
10.4k
                                    switch(s[6]) {
365
0
                                    default:
366
0
                                        break;
367
0
                                    case '-':
368
# if defined(FIPS_MODULE)
369
                                        if (ossl_likely(strcmp("check", s + 7) == 0)) {
370
                                            /* OSSL_KDF_PARAM_FIPS_DIGEST_CHECK */
371
                                            if (ossl_unlikely(r->ind_d != NULL)) {
372
                                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
373
                                                               "param %s is repeated", s);
374
                                                return 0;
375
                                            }
376
                                            r->ind_d = (OSSL_PARAM *)p;
377
                                        }
378
# endif
379
0
                                        break;
380
10.4k
                                    case '\0':
381
10.4k
                                        if (ossl_unlikely(r->digest != NULL)) {
382
0
                                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
383
0
                                                           "param %s is repeated", s);
384
0
                                            return 0;
385
0
                                        }
386
10.4k
                                        r->digest = (OSSL_PARAM *)p;
387
10.4k
                                    }
388
10.4k
                                }
389
10.4k
                            }
390
10.4k
                        }
391
10.4k
                    }
392
10.4k
                }
393
10.4k
                break;
394
10.4k
            case 'e':
395
0
                switch(s[1]) {
396
0
                default:
397
0
                    break;
398
0
                case 'm':
399
# if defined(FIPS_MODULE)
400
                    if (ossl_likely(strcmp("s_check", s + 2) == 0)) {
401
                        /* OSSL_KDF_PARAM_FIPS_EMS_CHECK */
402
                        if (ossl_unlikely(r->ind_e != NULL)) {
403
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
404
                                           "param %s is repeated", s);
405
                            return 0;
406
                        }
407
                        r->ind_e = (OSSL_PARAM *)p;
408
                    }
409
# endif
410
0
                    break;
411
0
                case 'n':
412
0
                    if (ossl_likely(strcmp("gine", s + 2) == 0)) {
413
                        /* OSSL_ALG_PARAM_ENGINE */
414
0
                        if (ossl_unlikely(r->engine != NULL)) {
415
0
                            ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
416
0
                                           "param %s is repeated", s);
417
0
                            return 0;
418
0
                        }
419
0
                        r->engine = (OSSL_PARAM *)p;
420
0
                    }
421
0
                }
422
0
                break;
423
0
            case 'k':
424
# if defined(FIPS_MODULE)
425
                if (ossl_likely(strcmp("ey-check", s + 1) == 0)) {
426
                    /* OSSL_KDF_PARAM_FIPS_KEY_CHECK */
427
                    if (ossl_unlikely(r->ind_k != NULL)) {
428
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
429
                                       "param %s is repeated", s);
430
                        return 0;
431
                    }
432
                    r->ind_k = (OSSL_PARAM *)p;
433
                }
434
# endif
435
0
                break;
436
520
            case 'p':
437
520
                if (ossl_likely(strcmp("roperties", s + 1) == 0)) {
438
                    /* OSSL_KDF_PARAM_PROPERTIES */
439
520
                    if (ossl_unlikely(r->propq != NULL)) {
440
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
441
0
                                       "param %s is repeated", s);
442
0
                        return 0;
443
0
                    }
444
520
                    r->propq = (OSSL_PARAM *)p;
445
520
                }
446
520
                break;
447
60.5k
            case 's':
448
60.5k
                switch(s[1]) {
449
0
                default:
450
0
                    break;
451
60.5k
                case 'e':
452
60.5k
                    switch(s[2]) {
453
0
                    default:
454
0
                        break;
455
10.4k
                    case 'c':
456
10.4k
                        if (ossl_likely(strcmp("ret", s + 3) == 0)) {
457
                            /* OSSL_KDF_PARAM_SECRET */
458
10.4k
                            if (ossl_unlikely(r->secret != NULL)) {
459
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
460
0
                                               "param %s is repeated", s);
461
0
                                return 0;
462
0
                            }
463
10.4k
                            r->secret = (OSSL_PARAM *)p;
464
10.4k
                        }
465
10.4k
                        break;
466
50.1k
                    case 'e':
467
50.1k
                        if (ossl_likely(strcmp("d", s + 3) == 0)) {
468
                            /* OSSL_KDF_PARAM_SEED */
469
50.1k
                            if (ossl_unlikely(r->num_seed >= TLSPRF_MAX_SEEDS)) {
470
0
                                ERR_raise_data(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS,
471
0
                                               "param %s present >%d times", s, TLSPRF_MAX_SEEDS);
472
0
                                return 0;
473
0
                            }
474
50.1k
                            r->seed[r->num_seed++] = (OSSL_PARAM *)p;
475
50.1k
                        }
476
60.5k
                    }
477
60.5k
                }
478
71.5k
            }
479
10.6k
    return 1;
480
10.6k
}
481
#endif
482
/* End of machine generated */
483
/* clang-format on */
484
485
static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
486
22.8k
{
487
22.8k
    struct tls1prf_set_ctx_params_st p;
488
22.8k
    TLS1_PRF *ctx = vctx;
489
22.8k
    OSSL_LIB_CTX *libctx;
490
491
22.8k
    if (ctx == NULL || !tls1prf_set_ctx_params_decoder(params, &p))
492
0
        return 0;
493
494
22.8k
    libctx = PROV_LIBCTX_OF(ctx->provctx);
495
496
22.8k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_e))
497
0
        return 0;
498
22.8k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_d))
499
0
        return 0;
500
22.8k
    if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE2, p.ind_k))
501
0
        return 0;
502
503
22.8k
    if (p.digest != NULL) {
504
22.4k
        PROV_DIGEST digest;
505
22.4k
        const EVP_MD *md = NULL;
506
22.4k
        const char *dgst;
507
508
22.4k
        if (!OSSL_PARAM_get_utf8_string_ptr(p.digest, &dgst))
509
0
            return 0;
510
511
22.4k
        if (OPENSSL_strcasecmp(dgst, OSSL_DIGEST_NAME_MD5_SHA1) == 0) {
512
7.52k
            if (!ossl_prov_macctx_load(&ctx->P_hash, NULL, NULL, NULL,
513
7.52k
                    p.propq, p.engine,
514
7.52k
                    OSSL_MAC_NAME_HMAC, NULL,
515
7.52k
                    OSSL_DIGEST_NAME_MD5, libctx))
516
4
                return 0;
517
7.52k
            if (!ossl_prov_macctx_load(&ctx->P_sha1, NULL, NULL, NULL,
518
7.52k
                    p.propq, p.engine,
519
7.52k
                    OSSL_MAC_NAME_HMAC, NULL,
520
7.52k
                    OSSL_DIGEST_NAME_SHA1, libctx))
521
0
                return 0;
522
14.9k
        } else {
523
14.9k
            EVP_MAC_CTX_free(ctx->P_sha1);
524
14.9k
            if (!ossl_prov_macctx_load(&ctx->P_hash, NULL, NULL, p.digest,
525
14.9k
                    p.propq, p.engine,
526
14.9k
                    OSSL_MAC_NAME_HMAC, NULL, NULL, libctx))
527
403
                return 0;
528
14.9k
        }
529
530
22.0k
        memset(&digest, 0, sizeof(digest));
531
22.0k
        if (!ossl_prov_digest_load(&digest, p.digest, p.propq, p.engine, libctx))
532
0
            return 0;
533
534
22.0k
        md = ossl_prov_digest_md(&digest);
535
22.0k
        if (EVP_MD_xof(md)) {
536
2
            ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
537
2
            ossl_prov_digest_reset(&digest);
538
2
            return 0;
539
2
        }
540
541
#ifdef FIPS_MODULE
542
        if (!fips_digest_check_passed(ctx, md)) {
543
            ossl_prov_digest_reset(&digest);
544
            return 0;
545
        }
546
#endif
547
548
22.0k
        ossl_prov_digest_reset(&digest);
549
22.0k
    }
550
551
22.4k
    if (p.secret != NULL) {
552
22.0k
        OPENSSL_clear_free(ctx->sec, ctx->seclen);
553
22.0k
        ctx->sec = NULL;
554
22.0k
        if (!OSSL_PARAM_get_octet_string(p.secret, (void **)&ctx->sec, 0,
555
22.0k
                &ctx->seclen))
556
0
            return 0;
557
558
#ifdef FIPS_MODULE
559
        if (!fips_key_check_passed(ctx))
560
            return 0;
561
#endif
562
22.0k
    }
563
564
    /*
565
     * The seed fields concatenate across set calls, so process them all
566
     * but only reallocate once.
567
     */
568
22.4k
    if (p.num_seed > 0) {
569
22.0k
        const void *vals[TLSPRF_MAX_SEEDS];
570
22.0k
        size_t sizes[TLSPRF_MAX_SEEDS];
571
22.0k
        size_t seedlen = ctx->seedlen;
572
22.0k
        int i, n = 0;
573
574
130k
        for (i = 0; i < p.num_seed; i++) {
575
108k
            sizes[i] = 0;
576
108k
            vals[i] = NULL;
577
108k
            if (p.seed[i]->data_size != 0 && p.seed[i]->data != NULL) {
578
58.8k
                int err = 0;
579
580
58.8k
                if (!OSSL_PARAM_get_octet_string_ptr(p.seed[i],
581
58.8k
                        vals + n, sizes + n))
582
0
                    return 0;
583
584
58.8k
                seedlen = safe_add_size_t(seedlen, sizes[n], &err);
585
58.8k
                if (err)
586
0
                    return 0;
587
58.8k
                n++;
588
58.8k
            }
589
108k
        }
590
591
22.0k
        if (seedlen != ctx->seedlen) {
592
21.8k
            unsigned char *seed = OPENSSL_clear_realloc(ctx->seed,
593
21.8k
                ctx->seedlen, seedlen);
594
595
21.8k
            if (seed == NULL)
596
0
                return 0;
597
21.8k
            ctx->seed = seed;
598
599
            /* No errors are possible, so copy them across */
600
80.6k
            for (i = 0; i < n; i++) {
601
58.8k
                memcpy(ctx->seed + ctx->seedlen, vals[i], sizes[i]);
602
58.8k
                ctx->seedlen += sizes[i];
603
58.8k
            }
604
21.8k
        }
605
22.0k
    }
606
607
22.4k
    return 1;
608
22.4k
}
609
610
static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
611
    ossl_unused void *ctx, ossl_unused void *provctx)
612
2.39k
{
613
2.39k
    return tls1prf_set_ctx_params_list;
614
2.39k
}
615
616
/* clang-format off */
617
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
618
#ifndef tls1prf_get_ctx_params_list
619
static const OSSL_PARAM tls1prf_get_ctx_params_list[] = {
620
    OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
621
# if defined(FIPS_MODULE)
622
    OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL),
623
# endif
624
    OSSL_PARAM_END
625
};
626
#endif
627
628
#ifndef tls1prf_get_ctx_params_st
629
struct tls1prf_get_ctx_params_st {
630
# if defined(FIPS_MODULE)
631
    OSSL_PARAM *ind;
632
# endif
633
    OSSL_PARAM *size;
634
};
635
#endif
636
637
#ifndef tls1prf_get_ctx_params_decoder
638
static int tls1prf_get_ctx_params_decoder
639
    (const OSSL_PARAM *p, struct tls1prf_get_ctx_params_st *r)
640
0
{
641
0
    const char *s;
642
643
0
    memset(r, 0, sizeof(*r));
644
0
    if (p != NULL)
645
0
        for (; (s = p->key) != NULL; p++)
646
0
            switch(s[0]) {
647
0
            default:
648
0
                break;
649
0
            case 'f':
650
# if defined(FIPS_MODULE)
651
                if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) {
652
                    /* OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR */
653
                    if (ossl_unlikely(r->ind != NULL)) {
654
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
655
                                       "param %s is repeated", s);
656
                        return 0;
657
                    }
658
                    r->ind = (OSSL_PARAM *)p;
659
                }
660
# endif
661
0
                break;
662
0
            case 's':
663
0
                if (ossl_likely(strcmp("ize", s + 1) == 0)) {
664
                    /* OSSL_KDF_PARAM_SIZE */
665
0
                    if (ossl_unlikely(r->size != NULL)) {
666
0
                        ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
667
0
                                       "param %s is repeated", s);
668
0
                        return 0;
669
0
                    }
670
0
                    r->size = (OSSL_PARAM *)p;
671
0
                }
672
0
            }
673
0
    return 1;
674
0
}
675
#endif
676
/* End of machine generated */
677
/* clang-format on */
678
679
static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
680
0
{
681
0
    struct tls1prf_get_ctx_params_st p;
682
0
    TLS1_PRF *ctx = (TLS1_PRF *)vctx;
683
684
0
    if (ctx == NULL || !tls1prf_get_ctx_params_decoder(params, &p))
685
0
        return 0;
686
687
0
    if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX))
688
0
        return 0;
689
690
0
    if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind))
691
0
        return 0;
692
0
    return 1;
693
0
}
694
695
static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
696
    ossl_unused void *ctx, ossl_unused void *provctx)
697
0
{
698
0
    return tls1prf_get_ctx_params_list;
699
0
}
700
701
const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
702
    { OSSL_FUNC_KDF_NEWCTX, (void (*)(void))kdf_tls1_prf_new },
703
    { OSSL_FUNC_KDF_DUPCTX, (void (*)(void))kdf_tls1_prf_dup },
704
    { OSSL_FUNC_KDF_FREECTX, (void (*)(void))kdf_tls1_prf_free },
705
    { OSSL_FUNC_KDF_RESET, (void (*)(void))kdf_tls1_prf_reset },
706
    { OSSL_FUNC_KDF_DERIVE, (void (*)(void))kdf_tls1_prf_derive },
707
    { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
708
        (void (*)(void))kdf_tls1_prf_settable_ctx_params },
709
    { OSSL_FUNC_KDF_SET_CTX_PARAMS,
710
        (void (*)(void))kdf_tls1_prf_set_ctx_params },
711
    { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
712
        (void (*)(void))kdf_tls1_prf_gettable_ctx_params },
713
    { OSSL_FUNC_KDF_GET_CTX_PARAMS,
714
        (void (*)(void))kdf_tls1_prf_get_ctx_params },
715
    OSSL_DISPATCH_END
716
};
717
718
/*
719
 * Refer to "The TLS Protocol Version 1.0" Section 5
720
 * (https://tools.ietf.org/html/rfc2246#section-5) and
721
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
722
 * (https://tools.ietf.org/html/rfc5246#section-5).
723
 *
724
 * P_<hash> is an expansion function that uses a single hash function to expand
725
 * a secret and seed into an arbitrary quantity of output:
726
 *
727
 *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
728
 *                            HMAC_<hash>(secret, A(2) + seed) +
729
 *                            HMAC_<hash>(secret, A(3) + seed) + ...
730
 *
731
 * where + indicates concatenation.  P_<hash> can be iterated as many times as
732
 * is necessary to produce the required quantity of data.
733
 *
734
 * A(i) is defined as:
735
 *     A(0) = seed
736
 *     A(i) = HMAC_<hash>(secret, A(i-1))
737
 */
738
static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
739
    const unsigned char *sec, size_t sec_len,
740
    const unsigned char *seed, size_t seed_len,
741
    unsigned char *out, size_t olen)
742
90.1k
{
743
90.1k
    size_t chunk;
744
90.1k
    EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
745
90.1k
    unsigned char Ai[EVP_MAX_MD_SIZE];
746
90.1k
    size_t Ai_len;
747
90.1k
    int ret = 0;
748
749
90.1k
    if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
750
0
        goto err;
751
90.1k
    chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
752
90.1k
    if (chunk == 0)
753
10
        goto err;
754
    /* A(0) = seed */
755
90.1k
    ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
756
90.1k
    if (ctx_Ai == NULL)
757
0
        goto err;
758
90.1k
    if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
759
0
        goto err;
760
761
238k
    for (;;) {
762
        /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
763
238k
        if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
764
0
            goto err;
765
238k
        EVP_MAC_CTX_free(ctx_Ai);
766
238k
        ctx_Ai = NULL;
767
768
        /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
769
238k
        ctx = EVP_MAC_CTX_dup(ctx_init);
770
238k
        if (ctx == NULL)
771
0
            goto err;
772
238k
        if (!EVP_MAC_update(ctx, Ai, Ai_len))
773
0
            goto err;
774
        /* save state for calculating next A(i) value */
775
238k
        if (olen > chunk) {
776
147k
            ctx_Ai = EVP_MAC_CTX_dup(ctx);
777
147k
            if (ctx_Ai == NULL)
778
0
                goto err;
779
147k
        }
780
238k
        if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
781
0
            goto err;
782
238k
        if (olen <= chunk) {
783
            /* last chunk - use Ai as temp bounce buffer */
784
90.1k
            if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
785
0
                goto err;
786
90.1k
            memcpy(out, Ai, olen);
787
90.1k
            break;
788
90.1k
        }
789
147k
        if (!EVP_MAC_final(ctx, out, NULL, olen))
790
0
            goto err;
791
147k
        EVP_MAC_CTX_free(ctx);
792
147k
        ctx = NULL;
793
147k
        out += chunk;
794
147k
        olen -= chunk;
795
147k
    }
796
90.1k
    ret = 1;
797
90.1k
err:
798
90.1k
    EVP_MAC_CTX_free(ctx);
799
90.1k
    EVP_MAC_CTX_free(ctx_Ai);
800
90.1k
    OPENSSL_cleanse(Ai, sizeof(Ai));
801
90.1k
    return ret;
802
90.1k
}
803
804
/*
805
 * Refer to "The TLS Protocol Version 1.0" Section 5
806
 * (https://tools.ietf.org/html/rfc2246#section-5) and
807
 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
808
 * (https://tools.ietf.org/html/rfc5246#section-5).
809
 *
810
 * For TLS v1.0 and TLS v1.1:
811
 *
812
 *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
813
 *                              P_SHA-1(S2, label + seed)
814
 *
815
 * S1 is taken from the first half of the secret, S2 from the second half.
816
 *
817
 *   L_S = length in bytes of secret;
818
 *   L_S1 = L_S2 = ceil(L_S / 2);
819
 *
820
 * For TLS v1.2:
821
 *
822
 *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)
823
 */
824
static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
825
    const unsigned char *sec, size_t slen,
826
    const unsigned char *seed, size_t seed_len,
827
    unsigned char *out, size_t olen)
828
68.6k
{
829
68.6k
    if (sha1ctx != NULL) {
830
        /* TLS v1.0 and TLS v1.1 */
831
21.5k
        size_t i;
832
21.5k
        unsigned char *tmp;
833
        /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
834
21.5k
        size_t L_S1 = (slen + 1) / 2;
835
21.5k
        size_t L_S2 = L_S1;
836
837
21.5k
        if (!tls1_prf_P_hash(mdctx, sec, L_S1,
838
21.5k
                seed, seed_len, out, olen))
839
0
            return 0;
840
841
21.5k
        if ((tmp = OPENSSL_malloc(olen)) == NULL)
842
0
            return 0;
843
844
21.5k
        if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
845
21.5k
                seed, seed_len, tmp, olen)) {
846
0
            OPENSSL_clear_free(tmp, olen);
847
0
            return 0;
848
0
        }
849
1.17M
        for (i = 0; i < olen; i++)
850
1.14M
            out[i] ^= tmp[i];
851
21.5k
        OPENSSL_clear_free(tmp, olen);
852
21.5k
        return 1;
853
21.5k
    }
854
855
    /* TLS v1.2 */
856
47.1k
    if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
857
10
        return 0;
858
859
47.0k
    return 1;
860
47.1k
}