/src/openssl/providers/implementations/kdfs/tls1_prf.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2016-2024 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  |  | #define TLS_MD_MASTER_SECRET_CONST        "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"  | 
92  |  | #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  |  |  | 
110  |  |     OSSL_FIPS_IND_DECLARE  | 
111  |  | } TLS1_PRF;  | 
112  |  |  | 
113  |  | static void *kdf_tls1_prf_new(void *provctx)  | 
114  | 0  | { | 
115  | 0  |     TLS1_PRF *ctx;  | 
116  |  | 
  | 
117  | 0  |     if (!ossl_prov_is_running())  | 
118  | 0  |         return NULL;  | 
119  |  |  | 
120  | 0  |     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { | 
121  | 0  |         ctx->provctx = provctx;  | 
122  | 0  |         OSSL_FIPS_IND_INIT(ctx)  | 
123  | 0  |     }  | 
124  | 0  |     return ctx;  | 
125  | 0  | }  | 
126  |  |  | 
127  |  | static void kdf_tls1_prf_free(void *vctx)  | 
128  | 0  | { | 
129  | 0  |     TLS1_PRF *ctx = (TLS1_PRF *)vctx;  | 
130  |  | 
  | 
131  | 0  |     if (ctx != NULL) { | 
132  | 0  |         kdf_tls1_prf_reset(ctx);  | 
133  | 0  |         OPENSSL_free(ctx);  | 
134  | 0  |     }  | 
135  | 0  | }  | 
136  |  |  | 
137  |  | static void kdf_tls1_prf_reset(void *vctx)  | 
138  | 0  | { | 
139  | 0  |     TLS1_PRF *ctx = (TLS1_PRF *)vctx;  | 
140  | 0  |     void *provctx = ctx->provctx;  | 
141  |  | 
  | 
142  | 0  |     EVP_MAC_CTX_free(ctx->P_hash);  | 
143  | 0  |     EVP_MAC_CTX_free(ctx->P_sha1);  | 
144  | 0  |     OPENSSL_clear_free(ctx->sec, ctx->seclen);  | 
145  | 0  |     OPENSSL_clear_free(ctx->seed, ctx->seedlen);  | 
146  | 0  |     memset(ctx, 0, sizeof(*ctx));  | 
147  | 0  |     ctx->provctx = provctx;  | 
148  | 0  | }  | 
149  |  |  | 
150  |  | static void *kdf_tls1_prf_dup(void *vctx)  | 
151  | 0  | { | 
152  | 0  |     const TLS1_PRF *src = (const TLS1_PRF *)vctx;  | 
153  | 0  |     TLS1_PRF *dest;  | 
154  |  | 
  | 
155  | 0  |     dest = kdf_tls1_prf_new(src->provctx);  | 
156  | 0  |     if (dest != NULL) { | 
157  | 0  |         if (src->P_hash != NULL  | 
158  | 0  |                     && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)  | 
159  | 0  |             goto err;  | 
160  | 0  |         if (src->P_sha1 != NULL  | 
161  | 0  |                     && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)  | 
162  | 0  |             goto err;  | 
163  | 0  |         if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))  | 
164  | 0  |             goto err;  | 
165  | 0  |         if (!ossl_prov_memdup(src->seed, src->seedlen, &dest->seed,  | 
166  | 0  |                               &dest->seedlen))  | 
167  | 0  |             goto err;  | 
168  | 0  |         OSSL_FIPS_IND_COPY(dest, src)  | 
169  | 0  |     }  | 
170  | 0  |     return dest;  | 
171  |  |  | 
172  | 0  |  err:  | 
173  | 0  |     kdf_tls1_prf_free(dest);  | 
174  | 0  |     return NULL;  | 
175  | 0  | }  | 
176  |  |  | 
177  |  | #ifdef FIPS_MODULE  | 
178  |  |  | 
179  |  | static int fips_ems_check_passed(TLS1_PRF *ctx)  | 
180  |  | { | 
181  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
182  |  |     /*  | 
183  |  |      * Check that TLS is using EMS.  | 
184  |  |      *  | 
185  |  |      * The seed buffer is prepended with a label.  | 
186  |  |      * If EMS mode is enforced then the label "master secret" is not allowed,  | 
187  |  |      * We do the check this way since the PRF is used for other purposes, as well  | 
188  |  |      * as "extended master secret".  | 
189  |  |      */  | 
190  |  |     int ems_approved = (ctx->seedlen < TLS_MD_MASTER_SECRET_CONST_SIZE  | 
191  |  |                        || memcmp(ctx->seed, TLS_MD_MASTER_SECRET_CONST,  | 
192  |  |                                  TLS_MD_MASTER_SECRET_CONST_SIZE) != 0);  | 
193  |  |  | 
194  |  |     if (!ems_approved) { | 
195  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,  | 
196  |  |                                          libctx, "TLS_PRF", "EMS",  | 
197  |  |                                          ossl_fips_config_tls1_prf_ems_check)) { | 
198  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_EMS_NOT_ENABLED);  | 
199  |  |             return 0;  | 
200  |  |         }  | 
201  |  |     }  | 
202  |  |     return 1;  | 
203  |  | }  | 
204  |  |  | 
205  |  | static int fips_digest_check_passed(TLS1_PRF *ctx, const EVP_MD *md)  | 
206  |  | { | 
207  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
208  |  |     /*  | 
209  |  |      * Perform digest check  | 
210  |  |      *  | 
211  |  |      * According to NIST SP 800-135r1 section 5.2, the valid hash functions are  | 
212  |  |      * specified in FIPS 180-3. ACVP also only lists the same set of hash  | 
213  |  |      * functions.  | 
214  |  |      */  | 
215  |  |     int digest_unapproved = !EVP_MD_is_a(md, SN_sha256)  | 
216  |  |         && !EVP_MD_is_a(md, SN_sha384)  | 
217  |  |         && !EVP_MD_is_a(md, SN_sha512);  | 
218  |  |  | 
219  |  |     if (digest_unapproved) { | 
220  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1,  | 
221  |  |                                          libctx, "TLS_PRF", "Digest",  | 
222  |  |                                          ossl_fips_config_tls1_prf_digest_check)) { | 
223  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED);  | 
224  |  |             return 0;  | 
225  |  |         }  | 
226  |  |     }  | 
227  |  |     return 1;  | 
228  |  | }  | 
229  |  |  | 
230  |  | static int fips_key_check_passed(TLS1_PRF *ctx)  | 
231  |  | { | 
232  |  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
233  |  |     int key_approved = ossl_kdf_check_key_size(ctx->seclen);  | 
234  |  |  | 
235  |  |     if (!key_approved) { | 
236  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,  | 
237  |  |                                          libctx, "TLS_PRF", "Key size",  | 
238  |  |                                          ossl_fips_config_tls1_prf_key_check)) { | 
239  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
240  |  |             return 0;  | 
241  |  |         }  | 
242  |  |     }  | 
243  |  |     return 1;  | 
244  |  | }  | 
245  |  | #endif  | 
246  |  |  | 
247  |  | static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,  | 
248  |  |                                const OSSL_PARAM params[])  | 
249  | 0  | { | 
250  | 0  |     TLS1_PRF *ctx = (TLS1_PRF *)vctx;  | 
251  |  | 
  | 
252  | 0  |     if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))  | 
253  | 0  |         return 0;  | 
254  |  |  | 
255  | 0  |     if (ctx->P_hash == NULL) { | 
256  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);  | 
257  | 0  |         return 0;  | 
258  | 0  |     }  | 
259  | 0  |     if (ctx->sec == NULL) { | 
260  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);  | 
261  | 0  |         return 0;  | 
262  | 0  |     }  | 
263  | 0  |     if (ctx->seedlen == 0) { | 
264  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);  | 
265  | 0  |         return 0;  | 
266  | 0  |     }  | 
267  | 0  |     if (keylen == 0) { | 
268  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
269  | 0  |         return 0;  | 
270  | 0  |     }  | 
271  |  |  | 
272  |  | #ifdef FIPS_MODULE  | 
273  |  |     if (!fips_ems_check_passed(ctx))  | 
274  |  |         return 0;  | 
275  |  | #endif  | 
276  |  |  | 
277  | 0  |     return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,  | 
278  | 0  |                         ctx->sec, ctx->seclen,  | 
279  | 0  |                         ctx->seed, ctx->seedlen,  | 
280  | 0  |                         key, keylen);  | 
281  | 0  | }  | 
282  |  |  | 
283  |  | static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
284  | 0  | { | 
285  | 0  |     const OSSL_PARAM *p;  | 
286  | 0  |     TLS1_PRF *ctx = vctx;  | 
287  | 0  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
288  |  | 
  | 
289  | 0  |     if (ossl_param_is_empty(params))  | 
290  | 0  |         return 1;  | 
291  |  |  | 
292  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params,  | 
293  | 0  |                                      OSSL_KDF_PARAM_FIPS_EMS_CHECK))  | 
294  | 0  |         return 0;  | 
295  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, params,  | 
296  | 0  |                                      OSSL_KDF_PARAM_FIPS_DIGEST_CHECK))  | 
297  | 0  |         return 0;  | 
298  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE2, params,  | 
299  | 0  |                                      OSSL_KDF_PARAM_FIPS_KEY_CHECK))  | 
300  | 0  |         return 0;  | 
301  |  |  | 
302  | 0  |     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) { | 
303  | 0  |         PROV_DIGEST digest;  | 
304  | 0  |         const EVP_MD *md = NULL;  | 
305  |  | 
  | 
306  | 0  |         if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) { | 
307  | 0  |             if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,  | 
308  | 0  |                                                    OSSL_MAC_NAME_HMAC,  | 
309  | 0  |                                                    NULL, SN_md5, libctx)  | 
310  | 0  |                 || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,  | 
311  | 0  |                                                       OSSL_MAC_NAME_HMAC,  | 
312  | 0  |                                                       NULL, SN_sha1, libctx))  | 
313  | 0  |                 return 0;  | 
314  | 0  |         } else { | 
315  | 0  |             EVP_MAC_CTX_free(ctx->P_sha1);  | 
316  | 0  |             if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,  | 
317  | 0  |                                                    OSSL_MAC_NAME_HMAC,  | 
318  | 0  |                                                    NULL, NULL, libctx))  | 
319  | 0  |                 return 0;  | 
320  | 0  |         }  | 
321  |  |  | 
322  | 0  |         memset(&digest, 0, sizeof(digest));  | 
323  | 0  |         if (!ossl_prov_digest_load_from_params(&digest, params, libctx))  | 
324  | 0  |             return 0;  | 
325  |  |  | 
326  | 0  |         md = ossl_prov_digest_md(&digest);  | 
327  | 0  |         if (EVP_MD_xof(md)) { | 
328  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);  | 
329  | 0  |             ossl_prov_digest_reset(&digest);  | 
330  | 0  |             return 0;  | 
331  | 0  |         }  | 
332  |  |  | 
333  |  | #ifdef FIPS_MODULE  | 
334  |  |         if (!fips_digest_check_passed(ctx, md)) { | 
335  |  |             ossl_prov_digest_reset(&digest);  | 
336  |  |             return 0;  | 
337  |  |         }  | 
338  |  | #endif  | 
339  |  |  | 
340  | 0  |         ossl_prov_digest_reset(&digest);  | 
341  | 0  |     }  | 
342  |  |  | 
343  | 0  |     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) { | 
344  | 0  |         OPENSSL_clear_free(ctx->sec, ctx->seclen);  | 
345  | 0  |         ctx->sec = NULL;  | 
346  | 0  |         if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))  | 
347  | 0  |             return 0;  | 
348  |  | 
  | 
349  |  | #ifdef FIPS_MODULE  | 
350  |  |         if (!fips_key_check_passed(ctx))  | 
351  |  |             return 0;  | 
352  |  | #endif  | 
353  | 0  |     }  | 
354  |  |     /* The seed fields concatenate, so process them all */  | 
355  | 0  |     if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) { | 
356  | 0  |         for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,  | 
357  | 0  |                                                       OSSL_KDF_PARAM_SEED)) { | 
358  | 0  |             if (p->data_size != 0 && p->data != NULL) { | 
359  | 0  |                 const void *val = NULL;  | 
360  | 0  |                 size_t sz = 0;  | 
361  | 0  |                 unsigned char *seed;  | 
362  | 0  |                 size_t seedlen;  | 
363  | 0  |                 int err = 0;  | 
364  |  | 
  | 
365  | 0  |                 if (!OSSL_PARAM_get_octet_string_ptr(p, &val, &sz))  | 
366  | 0  |                     return 0;  | 
367  |  |  | 
368  | 0  |                 seedlen = safe_add_size_t(ctx->seedlen, sz, &err);  | 
369  | 0  |                 if (err)  | 
370  | 0  |                     return 0;  | 
371  |  |  | 
372  | 0  |                 seed = OPENSSL_clear_realloc(ctx->seed, ctx->seedlen, seedlen);  | 
373  | 0  |                 if (!seed)  | 
374  | 0  |                     return 0;  | 
375  |  |  | 
376  | 0  |                 ctx->seed = seed;  | 
377  | 0  |                 if (ossl_assert(sz != 0))  | 
378  | 0  |                     memcpy(ctx->seed + ctx->seedlen, val, sz);  | 
379  | 0  |                 ctx->seedlen = seedlen;  | 
380  | 0  |             }  | 
381  | 0  |         }  | 
382  | 0  |     }  | 
383  | 0  |     return 1;  | 
384  | 0  | }  | 
385  |  |  | 
386  |  | static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(  | 
387  |  |         ossl_unused void *ctx, ossl_unused void *provctx)  | 
388  | 0  | { | 
389  | 0  |     static const OSSL_PARAM known_settable_ctx_params[] = { | 
390  | 0  |         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),  | 
391  | 0  |         OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),  | 
392  | 0  |         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),  | 
393  | 0  |         OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),  | 
394  | 0  |         OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_EMS_CHECK)  | 
395  | 0  |         OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK)  | 
396  | 0  |         OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_KDF_PARAM_FIPS_KEY_CHECK)  | 
397  | 0  |         OSSL_PARAM_END  | 
398  | 0  |     };  | 
399  | 0  |     return known_settable_ctx_params;  | 
400  | 0  | }  | 
401  |  |  | 
402  |  | static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])  | 
403  | 0  | { | 
404  | 0  |     OSSL_PARAM *p;  | 
405  |  | 
  | 
406  | 0  |     if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) { | 
407  | 0  |         if (!OSSL_PARAM_set_size_t(p, SIZE_MAX))  | 
408  | 0  |             return 0;  | 
409  | 0  |     }  | 
410  | 0  |     if (!OSSL_FIPS_IND_GET_CTX_PARAM(((TLS1_PRF *)vctx), params))  | 
411  | 0  |         return 0;  | 
412  | 0  |     return 1;  | 
413  | 0  | }  | 
414  |  |  | 
415  |  | static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(  | 
416  |  |         ossl_unused void *ctx, ossl_unused void *provctx)  | 
417  | 0  | { | 
418  | 0  |     static const OSSL_PARAM known_gettable_ctx_params[] = { | 
419  | 0  |         OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),  | 
420  | 0  |         OSSL_FIPS_IND_GETTABLE_CTX_PARAM()  | 
421  | 0  |         OSSL_PARAM_END  | 
422  | 0  |     };  | 
423  | 0  |     return known_gettable_ctx_params;  | 
424  | 0  | }  | 
425  |  |  | 
426  |  | const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = { | 
427  |  |     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new }, | 
428  |  |     { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_tls1_prf_dup }, | 
429  |  |     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free }, | 
430  |  |     { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset }, | 
431  |  |     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive }, | 
432  |  |     { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | 
433  |  |       (void(*)(void))kdf_tls1_prf_settable_ctx_params },  | 
434  |  |     { OSSL_FUNC_KDF_SET_CTX_PARAMS, | 
435  |  |       (void(*)(void))kdf_tls1_prf_set_ctx_params },  | 
436  |  |     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | 
437  |  |       (void(*)(void))kdf_tls1_prf_gettable_ctx_params },  | 
438  |  |     { OSSL_FUNC_KDF_GET_CTX_PARAMS, | 
439  |  |       (void(*)(void))kdf_tls1_prf_get_ctx_params },  | 
440  |  |     OSSL_DISPATCH_END  | 
441  |  | };  | 
442  |  |  | 
443  |  | /*  | 
444  |  |  * Refer to "The TLS Protocol Version 1.0" Section 5  | 
445  |  |  * (https://tools.ietf.org/html/rfc2246#section-5) and  | 
446  |  |  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5  | 
447  |  |  * (https://tools.ietf.org/html/rfc5246#section-5).  | 
448  |  |  *  | 
449  |  |  * P_<hash> is an expansion function that uses a single hash function to expand  | 
450  |  |  * a secret and seed into an arbitrary quantity of output:  | 
451  |  |  *  | 
452  |  |  *   P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +  | 
453  |  |  *                            HMAC_<hash>(secret, A(2) + seed) +  | 
454  |  |  *                            HMAC_<hash>(secret, A(3) + seed) + ...  | 
455  |  |  *  | 
456  |  |  * where + indicates concatenation.  P_<hash> can be iterated as many times as  | 
457  |  |  * is necessary to produce the required quantity of data.  | 
458  |  |  *  | 
459  |  |  * A(i) is defined as:  | 
460  |  |  *     A(0) = seed  | 
461  |  |  *     A(i) = HMAC_<hash>(secret, A(i-1))  | 
462  |  |  */  | 
463  |  | static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,  | 
464  |  |                            const unsigned char *sec, size_t sec_len,  | 
465  |  |                            const unsigned char *seed, size_t seed_len,  | 
466  |  |                            unsigned char *out, size_t olen)  | 
467  | 0  | { | 
468  | 0  |     size_t chunk;  | 
469  | 0  |     EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;  | 
470  | 0  |     unsigned char Ai[EVP_MAX_MD_SIZE];  | 
471  | 0  |     size_t Ai_len;  | 
472  | 0  |     int ret = 0;  | 
473  |  | 
  | 
474  | 0  |     if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))  | 
475  | 0  |         goto err;  | 
476  | 0  |     chunk = EVP_MAC_CTX_get_mac_size(ctx_init);  | 
477  | 0  |     if (chunk == 0)  | 
478  | 0  |         goto err;  | 
479  |  |     /* A(0) = seed */  | 
480  | 0  |     ctx_Ai = EVP_MAC_CTX_dup(ctx_init);  | 
481  | 0  |     if (ctx_Ai == NULL)  | 
482  | 0  |         goto err;  | 
483  | 0  |     if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))  | 
484  | 0  |         goto err;  | 
485  |  |  | 
486  | 0  |     for (;;) { | 
487  |  |         /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */  | 
488  | 0  |         if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))  | 
489  | 0  |             goto err;  | 
490  | 0  |         EVP_MAC_CTX_free(ctx_Ai);  | 
491  | 0  |         ctx_Ai = NULL;  | 
492  |  |  | 
493  |  |         /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */  | 
494  | 0  |         ctx = EVP_MAC_CTX_dup(ctx_init);  | 
495  | 0  |         if (ctx == NULL)  | 
496  | 0  |             goto err;  | 
497  | 0  |         if (!EVP_MAC_update(ctx, Ai, Ai_len))  | 
498  | 0  |             goto err;  | 
499  |  |         /* save state for calculating next A(i) value */  | 
500  | 0  |         if (olen > chunk) { | 
501  | 0  |             ctx_Ai = EVP_MAC_CTX_dup(ctx);  | 
502  | 0  |             if (ctx_Ai == NULL)  | 
503  | 0  |                 goto err;  | 
504  | 0  |         }  | 
505  | 0  |         if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))  | 
506  | 0  |             goto err;  | 
507  | 0  |         if (olen <= chunk) { | 
508  |  |             /* last chunk - use Ai as temp bounce buffer */  | 
509  | 0  |             if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))  | 
510  | 0  |                 goto err;  | 
511  | 0  |             memcpy(out, Ai, olen);  | 
512  | 0  |             break;  | 
513  | 0  |         }  | 
514  | 0  |         if (!EVP_MAC_final(ctx, out, NULL, olen))  | 
515  | 0  |             goto err;  | 
516  | 0  |         EVP_MAC_CTX_free(ctx);  | 
517  | 0  |         ctx = NULL;  | 
518  | 0  |         out += chunk;  | 
519  | 0  |         olen -= chunk;  | 
520  | 0  |     }  | 
521  | 0  |     ret = 1;  | 
522  | 0  |  err:  | 
523  | 0  |     EVP_MAC_CTX_free(ctx);  | 
524  | 0  |     EVP_MAC_CTX_free(ctx_Ai);  | 
525  | 0  |     OPENSSL_cleanse(Ai, sizeof(Ai));  | 
526  | 0  |     return ret;  | 
527  | 0  | }  | 
528  |  |  | 
529  |  | /*  | 
530  |  |  * Refer to "The TLS Protocol Version 1.0" Section 5  | 
531  |  |  * (https://tools.ietf.org/html/rfc2246#section-5) and  | 
532  |  |  * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5  | 
533  |  |  * (https://tools.ietf.org/html/rfc5246#section-5).  | 
534  |  |  *  | 
535  |  |  * For TLS v1.0 and TLS v1.1:  | 
536  |  |  *  | 
537  |  |  *   PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR  | 
538  |  |  *                              P_SHA-1(S2, label + seed)  | 
539  |  |  *  | 
540  |  |  * S1 is taken from the first half of the secret, S2 from the second half.  | 
541  |  |  *  | 
542  |  |  *   L_S = length in bytes of secret;  | 
543  |  |  *   L_S1 = L_S2 = ceil(L_S / 2);  | 
544  |  |  *  | 
545  |  |  * For TLS v1.2:  | 
546  |  |  *  | 
547  |  |  *   PRF(secret, label, seed) = P_<hash>(secret, label + seed)  | 
548  |  |  */  | 
549  |  | static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,  | 
550  |  |                         const unsigned char *sec, size_t slen,  | 
551  |  |                         const unsigned char *seed, size_t seed_len,  | 
552  |  |                         unsigned char *out, size_t olen)  | 
553  | 0  | { | 
554  | 0  |     if (sha1ctx != NULL) { | 
555  |  |         /* TLS v1.0 and TLS v1.1 */  | 
556  | 0  |         size_t i;  | 
557  | 0  |         unsigned char *tmp;  | 
558  |  |         /* calc: L_S1 = L_S2 = ceil(L_S / 2) */  | 
559  | 0  |         size_t L_S1 = (slen + 1) / 2;  | 
560  | 0  |         size_t L_S2 = L_S1;  | 
561  |  | 
  | 
562  | 0  |         if (!tls1_prf_P_hash(mdctx, sec, L_S1,  | 
563  | 0  |                              seed, seed_len, out, olen))  | 
564  | 0  |             return 0;  | 
565  |  |  | 
566  | 0  |         if ((tmp = OPENSSL_malloc(olen)) == NULL)  | 
567  | 0  |             return 0;  | 
568  |  |  | 
569  | 0  |         if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,  | 
570  | 0  |                              seed, seed_len, tmp, olen)) { | 
571  | 0  |             OPENSSL_clear_free(tmp, olen);  | 
572  | 0  |             return 0;  | 
573  | 0  |         }  | 
574  | 0  |         for (i = 0; i < olen; i++)  | 
575  | 0  |             out[i] ^= tmp[i];  | 
576  | 0  |         OPENSSL_clear_free(tmp, olen);  | 
577  | 0  |         return 1;  | 
578  | 0  |     }  | 
579  |  |  | 
580  |  |     /* TLS v1.2 */  | 
581  | 0  |     if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))  | 
582  | 0  |         return 0;  | 
583  |  |  | 
584  | 0  |     return 1;  | 
585  | 0  | }  |