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