/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 | 45.6k | { |
119 | 45.6k | TLS1_PRF *ctx; |
120 | | |
121 | 45.6k | if (!ossl_prov_is_running()) |
122 | 0 | return NULL; |
123 | | |
124 | 45.6k | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { |
125 | 45.6k | ctx->provctx = provctx; |
126 | 45.6k | OSSL_FIPS_IND_INIT(ctx) |
127 | 45.6k | } |
128 | 45.6k | return ctx; |
129 | 45.6k | } |
130 | | |
131 | | static void kdf_tls1_prf_free(void *vctx) |
132 | 49.8k | { |
133 | 49.8k | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
134 | | |
135 | 49.8k | if (ctx != NULL) { |
136 | 49.8k | kdf_tls1_prf_reset(ctx); |
137 | 49.8k | OPENSSL_free(ctx); |
138 | 49.8k | } |
139 | 49.8k | } |
140 | | |
141 | | static void kdf_tls1_prf_reset(void *vctx) |
142 | 49.8k | { |
143 | 49.8k | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
144 | 49.8k | void *provctx = ctx->provctx; |
145 | | |
146 | 49.8k | EVP_MAC_CTX_free(ctx->P_hash); |
147 | 49.8k | EVP_MAC_CTX_free(ctx->P_sha1); |
148 | 49.8k | OPENSSL_clear_free(ctx->sec, ctx->seclen); |
149 | 49.8k | OPENSSL_clear_free(ctx->seed, ctx->seedlen); |
150 | 49.8k | memset(ctx, 0, sizeof(*ctx)); |
151 | 49.8k | ctx->provctx = provctx; |
152 | 49.8k | } |
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 | 35.9k | { |
254 | 35.9k | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
255 | | |
256 | 35.9k | if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params)) |
257 | 0 | return 0; |
258 | | |
259 | 35.9k | if (ctx->P_hash == NULL) { |
260 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
261 | 0 | return 0; |
262 | 0 | } |
263 | 35.9k | if (ctx->sec == NULL) { |
264 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); |
265 | 0 | return 0; |
266 | 0 | } |
267 | 35.9k | if (ctx->seedlen == 0) { |
268 | 362 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED); |
269 | 362 | return 0; |
270 | 362 | } |
271 | 35.6k | 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 | 35.6k | return tls1_prf_alg(ctx->P_hash, ctx->P_sha1, |
282 | 35.6k | ctx->sec, ctx->seclen, |
283 | 35.6k | ctx->seed, ctx->seedlen, |
284 | 35.6k | key, keylen); |
285 | 35.6k | } |
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 | 10.3k | { |
331 | 10.3k | const char *s; |
332 | | |
333 | 10.3k | memset(r, 0, sizeof(*r)); |
334 | 10.3k | if (p != NULL) |
335 | 80.0k | for (; (s = p->key) != NULL; p++) |
336 | 69.9k | switch(s[0]) { |
337 | 0 | default: |
338 | 0 | break; |
339 | 10.1k | case 'd': |
340 | 10.1k | switch(s[1]) { |
341 | 0 | default: |
342 | 0 | break; |
343 | 10.1k | case 'i': |
344 | 10.1k | switch(s[2]) { |
345 | 0 | default: |
346 | 0 | break; |
347 | 10.1k | case 'g': |
348 | 10.1k | switch(s[3]) { |
349 | 0 | default: |
350 | 0 | break; |
351 | 10.1k | case 'e': |
352 | 10.1k | switch(s[4]) { |
353 | 0 | default: |
354 | 0 | break; |
355 | 10.1k | case 's': |
356 | 10.1k | switch(s[5]) { |
357 | 0 | default: |
358 | 0 | break; |
359 | 10.1k | case 't': |
360 | 10.1k | 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 | | if (ossl_unlikely(r->ind_d != NULL)) { |
367 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
368 | | "param %s is repeated", s); |
369 | | return 0; |
370 | | } |
371 | | r->ind_d = (OSSL_PARAM *)p; |
372 | | } |
373 | | # endif |
374 | 0 | break; |
375 | 10.1k | case '\0': |
376 | 10.1k | if (ossl_unlikely(r->digest != NULL)) { |
377 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
378 | 0 | "param %s is repeated", s); |
379 | 0 | return 0; |
380 | 0 | } |
381 | 10.1k | r->digest = (OSSL_PARAM *)p; |
382 | 10.1k | } |
383 | 10.1k | } |
384 | 10.1k | } |
385 | 10.1k | } |
386 | 10.1k | } |
387 | 10.1k | } |
388 | 10.1k | break; |
389 | 10.1k | case 'e': |
390 | 0 | switch(s[1]) { |
391 | 0 | default: |
392 | 0 | break; |
393 | 0 | case 'm': |
394 | | # if defined(FIPS_MODULE) |
395 | | if (ossl_likely(strcmp("s_check", s + 2) == 0)) { |
396 | | if (ossl_unlikely(r->ind_e != NULL)) { |
397 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
398 | | "param %s is repeated", s); |
399 | | return 0; |
400 | | } |
401 | | r->ind_e = (OSSL_PARAM *)p; |
402 | | } |
403 | | # endif |
404 | 0 | break; |
405 | 0 | case 'n': |
406 | 0 | if (ossl_likely(strcmp("gine", s + 2) == 0)) { |
407 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
408 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
409 | 0 | "param %s is repeated", s); |
410 | 0 | return 0; |
411 | 0 | } |
412 | 0 | r->engine = (OSSL_PARAM *)p; |
413 | 0 | } |
414 | 0 | } |
415 | 0 | break; |
416 | 0 | case 'k': |
417 | | # if defined(FIPS_MODULE) |
418 | | if (ossl_likely(strcmp("ey-check", s + 1) == 0)) { |
419 | | if (ossl_unlikely(r->ind_k != NULL)) { |
420 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
421 | | "param %s is repeated", s); |
422 | | return 0; |
423 | | } |
424 | | r->ind_k = (OSSL_PARAM *)p; |
425 | | } |
426 | | # endif |
427 | 0 | break; |
428 | 433 | case 'p': |
429 | 433 | if (ossl_likely(strcmp("roperties", s + 1) == 0)) { |
430 | 433 | if (ossl_unlikely(r->propq != NULL)) { |
431 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
432 | 0 | "param %s is repeated", s); |
433 | 0 | return 0; |
434 | 0 | } |
435 | 433 | r->propq = (OSSL_PARAM *)p; |
436 | 433 | } |
437 | 433 | break; |
438 | 59.3k | case 's': |
439 | 59.3k | switch(s[1]) { |
440 | 0 | default: |
441 | 0 | break; |
442 | 59.3k | case 'e': |
443 | 59.3k | switch(s[2]) { |
444 | 0 | default: |
445 | 0 | break; |
446 | 10.1k | case 'c': |
447 | 10.1k | if (ossl_likely(strcmp("ret", s + 3) == 0)) { |
448 | 10.1k | if (ossl_unlikely(r->secret != NULL)) { |
449 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
450 | 0 | "param %s is repeated", s); |
451 | 0 | return 0; |
452 | 0 | } |
453 | 10.1k | r->secret = (OSSL_PARAM *)p; |
454 | 10.1k | } |
455 | 10.1k | break; |
456 | 49.1k | case 'e': |
457 | 49.1k | if (ossl_likely(strcmp("d", s + 3) == 0)) { |
458 | 49.1k | if (ossl_unlikely(r->num_seed >= TLSPRF_MAX_SEEDS)) { |
459 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS, |
460 | 0 | "param %s present >%d times", s, TLSPRF_MAX_SEEDS); |
461 | 0 | return 0; |
462 | 0 | } |
463 | 49.1k | r->seed[r->num_seed++] = (OSSL_PARAM *)p; |
464 | 49.1k | } |
465 | 59.3k | } |
466 | 59.3k | } |
467 | 69.9k | } |
468 | 10.3k | return 1; |
469 | 10.3k | } |
470 | | #endif |
471 | | /* End of machine generated */ |
472 | | |
473 | | static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
474 | 10.3k | { |
475 | 10.3k | struct tls1prf_set_ctx_params_st p; |
476 | 10.3k | TLS1_PRF *ctx = vctx; |
477 | 10.3k | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
478 | | |
479 | 10.3k | if (!tls1prf_set_ctx_params_decoder(params, &p)) |
480 | 0 | return 0; |
481 | | |
482 | 10.3k | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_e)) |
483 | 0 | return 0; |
484 | 10.3k | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_d)) |
485 | 0 | return 0; |
486 | 10.3k | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE2, p.ind_k)) |
487 | 0 | return 0; |
488 | | |
489 | 10.3k | if (p.digest != NULL) { |
490 | 10.1k | PROV_DIGEST digest; |
491 | 10.1k | const EVP_MD *md = NULL; |
492 | 10.1k | const char *dgst; |
493 | | |
494 | 10.1k | if (!OSSL_PARAM_get_utf8_string_ptr(p.digest, &dgst)) |
495 | 0 | return 0; |
496 | | |
497 | 10.1k | if (OPENSSL_strcasecmp(dgst, OSSL_DIGEST_NAME_MD5_SHA1) == 0) { |
498 | 2.84k | if (!ossl_prov_macctx_load(&ctx->P_hash, NULL, NULL, NULL, |
499 | 2.84k | p.propq, p.engine, |
500 | 2.84k | OSSL_MAC_NAME_HMAC, NULL, |
501 | 2.84k | OSSL_DIGEST_NAME_MD5, libctx)) |
502 | 1 | return 0; |
503 | 2.84k | if (!ossl_prov_macctx_load(&ctx->P_sha1, NULL, NULL, NULL, |
504 | 2.84k | p.propq, p.engine, |
505 | 2.84k | OSSL_MAC_NAME_HMAC, NULL, |
506 | 2.84k | OSSL_DIGEST_NAME_SHA1, libctx)) |
507 | 0 | return 0; |
508 | 7.32k | } else { |
509 | 7.32k | EVP_MAC_CTX_free(ctx->P_sha1); |
510 | 7.32k | if (!ossl_prov_macctx_load(&ctx->P_hash, NULL, NULL, p.digest, |
511 | 7.32k | p.propq, p.engine, |
512 | 7.32k | OSSL_MAC_NAME_HMAC, NULL, NULL, libctx)) |
513 | 233 | return 0; |
514 | 7.32k | } |
515 | | |
516 | 9.93k | memset(&digest, 0, sizeof(digest)); |
517 | 9.93k | if (!ossl_prov_digest_load(&digest, p.digest, p.propq, p.engine, libctx)) |
518 | 0 | return 0; |
519 | | |
520 | 9.93k | md = ossl_prov_digest_md(&digest); |
521 | 9.93k | if (EVP_MD_xof(md)) { |
522 | 1 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
523 | 1 | ossl_prov_digest_reset(&digest); |
524 | 1 | return 0; |
525 | 1 | } |
526 | | |
527 | | #ifdef FIPS_MODULE |
528 | | if (!fips_digest_check_passed(ctx, md)) { |
529 | | ossl_prov_digest_reset(&digest); |
530 | | return 0; |
531 | | } |
532 | | #endif |
533 | | |
534 | 9.93k | ossl_prov_digest_reset(&digest); |
535 | 9.93k | } |
536 | | |
537 | 10.1k | if (p.secret != NULL) { |
538 | 9.93k | OPENSSL_clear_free(ctx->sec, ctx->seclen); |
539 | 9.93k | ctx->sec = NULL; |
540 | 9.93k | if (!OSSL_PARAM_get_octet_string(p.secret, (void **)&ctx->sec, 0, |
541 | 9.93k | &ctx->seclen)) |
542 | 0 | return 0; |
543 | | |
544 | | #ifdef FIPS_MODULE |
545 | | if (!fips_key_check_passed(ctx)) |
546 | | return 0; |
547 | | #endif |
548 | 9.93k | } |
549 | | |
550 | | /* |
551 | | * The seed fields concatenate across set calls, so process them all |
552 | | * but only reallocate once. |
553 | | */ |
554 | 10.1k | if (p.num_seed > 0) { |
555 | 9.93k | const void *vals[TLSPRF_MAX_SEEDS]; |
556 | 9.93k | size_t sizes[TLSPRF_MAX_SEEDS]; |
557 | 9.93k | size_t seedlen = ctx->seedlen; |
558 | 9.93k | int i, n = 0; |
559 | | |
560 | 58.8k | for (i = 0; i < p.num_seed; i++) { |
561 | 48.8k | sizes[i] = 0; |
562 | 48.8k | vals[i] = NULL; |
563 | 48.8k | if (p.seed[i]->data_size != 0 && p.seed[i]->data != NULL) { |
564 | 26.5k | int err = 0; |
565 | | |
566 | 26.5k | if (!OSSL_PARAM_get_octet_string_ptr(p.seed[i], |
567 | 26.5k | vals + n, sizes + n)) |
568 | 0 | return 0; |
569 | | |
570 | 26.5k | seedlen = safe_add_size_t(seedlen, sizes[n], &err); |
571 | 26.5k | if (err) |
572 | 0 | return 0; |
573 | 26.5k | n++; |
574 | 26.5k | } |
575 | 48.8k | } |
576 | | |
577 | 9.93k | if (seedlen != ctx->seedlen) { |
578 | 9.82k | unsigned char *seed = OPENSSL_clear_realloc(ctx->seed, |
579 | 9.82k | ctx->seedlen, seedlen); |
580 | | |
581 | 9.82k | if (seed == NULL) |
582 | 0 | return 0; |
583 | 9.82k | ctx->seed = seed; |
584 | | |
585 | | /* No errors are possible, so copy them across */ |
586 | 36.3k | for (i = 0; i < n; i++) { |
587 | 26.5k | memcpy(ctx->seed + ctx->seedlen, vals[i], sizes[i]); |
588 | 26.5k | ctx->seedlen += sizes[i]; |
589 | 26.5k | } |
590 | 9.82k | } |
591 | 9.93k | } |
592 | | |
593 | 10.1k | return 1; |
594 | 10.1k | } |
595 | | |
596 | | static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params( |
597 | | ossl_unused void *ctx, ossl_unused void *provctx) |
598 | 1.55k | { |
599 | 1.55k | return tls1prf_set_ctx_params_list; |
600 | 1.55k | } |
601 | | |
602 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
603 | | #ifndef tls1prf_get_ctx_params_list |
604 | | static const OSSL_PARAM tls1prf_get_ctx_params_list[] = { |
605 | | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
606 | | # if defined(FIPS_MODULE) |
607 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
608 | | # endif |
609 | | OSSL_PARAM_END |
610 | | }; |
611 | | #endif |
612 | | |
613 | | #ifndef tls1prf_get_ctx_params_st |
614 | | struct tls1prf_get_ctx_params_st { |
615 | | # if defined(FIPS_MODULE) |
616 | | OSSL_PARAM *ind; |
617 | | # endif |
618 | | OSSL_PARAM *size; |
619 | | }; |
620 | | #endif |
621 | | |
622 | | #ifndef tls1prf_get_ctx_params_decoder |
623 | | static int tls1prf_get_ctx_params_decoder |
624 | | (const OSSL_PARAM *p, struct tls1prf_get_ctx_params_st *r) |
625 | 0 | { |
626 | 0 | const char *s; |
627 | |
|
628 | 0 | memset(r, 0, sizeof(*r)); |
629 | 0 | if (p != NULL) |
630 | 0 | for (; (s = p->key) != NULL; p++) |
631 | 0 | switch(s[0]) { |
632 | 0 | default: |
633 | 0 | break; |
634 | 0 | case 'f': |
635 | | # if defined(FIPS_MODULE) |
636 | | if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) { |
637 | | if (ossl_unlikely(r->ind != NULL)) { |
638 | | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
639 | | "param %s is repeated", s); |
640 | | return 0; |
641 | | } |
642 | | r->ind = (OSSL_PARAM *)p; |
643 | | } |
644 | | # endif |
645 | 0 | break; |
646 | 0 | case 's': |
647 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
648 | 0 | if (ossl_unlikely(r->size != NULL)) { |
649 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
650 | 0 | "param %s is repeated", s); |
651 | 0 | return 0; |
652 | 0 | } |
653 | 0 | r->size = (OSSL_PARAM *)p; |
654 | 0 | } |
655 | 0 | } |
656 | 0 | return 1; |
657 | 0 | } |
658 | | #endif |
659 | | /* End of machine generated */ |
660 | | |
661 | | static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
662 | 0 | { |
663 | 0 | struct tls1prf_get_ctx_params_st p; |
664 | 0 | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
665 | |
|
666 | 0 | if (ctx == NULL || !tls1prf_get_ctx_params_decoder(params, &p)) |
667 | 0 | return 0; |
668 | | |
669 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
670 | 0 | return 0; |
671 | | |
672 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
673 | 0 | return 0; |
674 | 0 | return 1; |
675 | 0 | } |
676 | | |
677 | | static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params( |
678 | | ossl_unused void *ctx, ossl_unused void *provctx) |
679 | 0 | { |
680 | 0 | return tls1prf_get_ctx_params_list; |
681 | 0 | } |
682 | | |
683 | | const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = { |
684 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new }, |
685 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_tls1_prf_dup }, |
686 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free }, |
687 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset }, |
688 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive }, |
689 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
690 | | (void(*)(void))kdf_tls1_prf_settable_ctx_params }, |
691 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, |
692 | | (void(*)(void))kdf_tls1_prf_set_ctx_params }, |
693 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
694 | | (void(*)(void))kdf_tls1_prf_gettable_ctx_params }, |
695 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, |
696 | | (void(*)(void))kdf_tls1_prf_get_ctx_params }, |
697 | | OSSL_DISPATCH_END |
698 | | }; |
699 | | |
700 | | /* |
701 | | * Refer to "The TLS Protocol Version 1.0" Section 5 |
702 | | * (https://tools.ietf.org/html/rfc2246#section-5) and |
703 | | * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 |
704 | | * (https://tools.ietf.org/html/rfc5246#section-5). |
705 | | * |
706 | | * P_<hash> is an expansion function that uses a single hash function to expand |
707 | | * a secret and seed into an arbitrary quantity of output: |
708 | | * |
709 | | * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) + |
710 | | * HMAC_<hash>(secret, A(2) + seed) + |
711 | | * HMAC_<hash>(secret, A(3) + seed) + ... |
712 | | * |
713 | | * where + indicates concatenation. P_<hash> can be iterated as many times as |
714 | | * is necessary to produce the required quantity of data. |
715 | | * |
716 | | * A(i) is defined as: |
717 | | * A(0) = seed |
718 | | * A(i) = HMAC_<hash>(secret, A(i-1)) |
719 | | */ |
720 | | static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init, |
721 | | const unsigned char *sec, size_t sec_len, |
722 | | const unsigned char *seed, size_t seed_len, |
723 | | unsigned char *out, size_t olen) |
724 | 60.6k | { |
725 | 60.6k | size_t chunk; |
726 | 60.6k | EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL; |
727 | 60.6k | unsigned char Ai[EVP_MAX_MD_SIZE]; |
728 | 60.6k | size_t Ai_len; |
729 | 60.6k | int ret = 0; |
730 | | |
731 | 60.6k | if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL)) |
732 | 0 | goto err; |
733 | 60.6k | chunk = EVP_MAC_CTX_get_mac_size(ctx_init); |
734 | 60.6k | if (chunk == 0) |
735 | 2 | goto err; |
736 | | /* A(0) = seed */ |
737 | 60.6k | ctx_Ai = EVP_MAC_CTX_dup(ctx_init); |
738 | 60.6k | if (ctx_Ai == NULL) |
739 | 0 | goto err; |
740 | 60.6k | if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len)) |
741 | 0 | goto err; |
742 | | |
743 | 155k | for (;;) { |
744 | | /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */ |
745 | 155k | if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai))) |
746 | 0 | goto err; |
747 | 155k | EVP_MAC_CTX_free(ctx_Ai); |
748 | 155k | ctx_Ai = NULL; |
749 | | |
750 | | /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */ |
751 | 155k | ctx = EVP_MAC_CTX_dup(ctx_init); |
752 | 155k | if (ctx == NULL) |
753 | 0 | goto err; |
754 | 155k | if (!EVP_MAC_update(ctx, Ai, Ai_len)) |
755 | 0 | goto err; |
756 | | /* save state for calculating next A(i) value */ |
757 | 155k | if (olen > chunk) { |
758 | 94.6k | ctx_Ai = EVP_MAC_CTX_dup(ctx); |
759 | 94.6k | if (ctx_Ai == NULL) |
760 | 0 | goto err; |
761 | 94.6k | } |
762 | 155k | if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len)) |
763 | 0 | goto err; |
764 | 155k | if (olen <= chunk) { |
765 | | /* last chunk - use Ai as temp bounce buffer */ |
766 | 60.6k | if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai))) |
767 | 0 | goto err; |
768 | 60.6k | memcpy(out, Ai, olen); |
769 | 60.6k | break; |
770 | 60.6k | } |
771 | 94.6k | if (!EVP_MAC_final(ctx, out, NULL, olen)) |
772 | 0 | goto err; |
773 | 94.6k | EVP_MAC_CTX_free(ctx); |
774 | 94.6k | ctx = NULL; |
775 | 94.6k | out += chunk; |
776 | 94.6k | olen -= chunk; |
777 | 94.6k | } |
778 | 60.6k | ret = 1; |
779 | 60.6k | err: |
780 | 60.6k | EVP_MAC_CTX_free(ctx); |
781 | 60.6k | EVP_MAC_CTX_free(ctx_Ai); |
782 | 60.6k | OPENSSL_cleanse(Ai, sizeof(Ai)); |
783 | 60.6k | return ret; |
784 | 60.6k | } |
785 | | |
786 | | /* |
787 | | * Refer to "The TLS Protocol Version 1.0" Section 5 |
788 | | * (https://tools.ietf.org/html/rfc2246#section-5) and |
789 | | * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 |
790 | | * (https://tools.ietf.org/html/rfc5246#section-5). |
791 | | * |
792 | | * For TLS v1.0 and TLS v1.1: |
793 | | * |
794 | | * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR |
795 | | * P_SHA-1(S2, label + seed) |
796 | | * |
797 | | * S1 is taken from the first half of the secret, S2 from the second half. |
798 | | * |
799 | | * L_S = length in bytes of secret; |
800 | | * L_S1 = L_S2 = ceil(L_S / 2); |
801 | | * |
802 | | * For TLS v1.2: |
803 | | * |
804 | | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
805 | | */ |
806 | | static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx, |
807 | | const unsigned char *sec, size_t slen, |
808 | | const unsigned char *seed, size_t seed_len, |
809 | | unsigned char *out, size_t olen) |
810 | 48.5k | { |
811 | 48.5k | if (sha1ctx != NULL) { |
812 | | /* TLS v1.0 and TLS v1.1 */ |
813 | 12.0k | size_t i; |
814 | 12.0k | unsigned char *tmp; |
815 | | /* calc: L_S1 = L_S2 = ceil(L_S / 2) */ |
816 | 12.0k | size_t L_S1 = (slen + 1) / 2; |
817 | 12.0k | size_t L_S2 = L_S1; |
818 | | |
819 | 12.0k | if (!tls1_prf_P_hash(mdctx, sec, L_S1, |
820 | 12.0k | seed, seed_len, out, olen)) |
821 | 0 | return 0; |
822 | | |
823 | 12.0k | if ((tmp = OPENSSL_malloc(olen)) == NULL) |
824 | 0 | return 0; |
825 | | |
826 | 12.0k | if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2, |
827 | 12.0k | seed, seed_len, tmp, olen)) { |
828 | 0 | OPENSSL_clear_free(tmp, olen); |
829 | 0 | return 0; |
830 | 0 | } |
831 | 684k | for (i = 0; i < olen; i++) |
832 | 672k | out[i] ^= tmp[i]; |
833 | 12.0k | OPENSSL_clear_free(tmp, olen); |
834 | 12.0k | return 1; |
835 | 12.0k | } |
836 | | |
837 | | /* TLS v1.2 */ |
838 | 36.4k | if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen)) |
839 | 2 | return 0; |
840 | | |
841 | 36.4k | return 1; |
842 | 36.4k | } |