/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 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_EMS_CHECK, NULL), |
295 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_DIGEST_CHECK, NULL), |
296 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_KEY_CHECK, NULL), |
297 | | OSSL_PARAM_END |
298 | | }; |
299 | | #endif |
300 | | |
301 | | #ifndef tls1prf_set_ctx_params_st |
302 | | struct tls1prf_set_ctx_params_st { |
303 | | OSSL_PARAM *digest; |
304 | | OSSL_PARAM *engine; |
305 | | OSSL_PARAM *ind_d; |
306 | | OSSL_PARAM *ind_e; |
307 | | OSSL_PARAM *ind_k; |
308 | | OSSL_PARAM *propq; |
309 | | OSSL_PARAM *secret; |
310 | | OSSL_PARAM *seed[TLSPRF_MAX_SEEDS]; |
311 | | int num_seed; |
312 | | }; |
313 | | #endif |
314 | | |
315 | | #ifndef tls1prf_set_ctx_params_decoder |
316 | | static int tls1prf_set_ctx_params_decoder |
317 | | (const OSSL_PARAM *p, struct tls1prf_set_ctx_params_st *r) |
318 | 0 | { |
319 | 0 | const char *s; |
320 | |
|
321 | 0 | memset(r, 0, sizeof(*r)); |
322 | 0 | if (p != NULL) |
323 | 0 | for (; (s = p->key) != NULL; p++) |
324 | 0 | switch(s[0]) { |
325 | 0 | default: |
326 | 0 | break; |
327 | 0 | case 'd': |
328 | 0 | switch(s[1]) { |
329 | 0 | default: |
330 | 0 | break; |
331 | 0 | case 'i': |
332 | 0 | switch(s[2]) { |
333 | 0 | default: |
334 | 0 | break; |
335 | 0 | case 'g': |
336 | 0 | switch(s[3]) { |
337 | 0 | default: |
338 | 0 | break; |
339 | 0 | case 'e': |
340 | 0 | switch(s[4]) { |
341 | 0 | default: |
342 | 0 | break; |
343 | 0 | case 's': |
344 | 0 | switch(s[5]) { |
345 | 0 | default: |
346 | 0 | break; |
347 | 0 | case 't': |
348 | 0 | switch(s[6]) { |
349 | 0 | default: |
350 | 0 | break; |
351 | 0 | case '-': |
352 | 0 | if (ossl_likely(strcmp("check", s + 7) == 0)) { |
353 | 0 | if (ossl_likely(r->ind_d == NULL)) |
354 | 0 | r->ind_d = (OSSL_PARAM *)p; |
355 | 0 | } |
356 | 0 | break; |
357 | 0 | case '\0': |
358 | 0 | if (ossl_likely(r->digest == NULL)) |
359 | 0 | r->digest = (OSSL_PARAM *)p; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | } |
363 | 0 | } |
364 | 0 | } |
365 | 0 | } |
366 | 0 | break; |
367 | 0 | case 'e': |
368 | 0 | switch(s[1]) { |
369 | 0 | default: |
370 | 0 | break; |
371 | 0 | case 'm': |
372 | 0 | if (ossl_likely(strcmp("s_check", s + 2) == 0)) { |
373 | 0 | if (ossl_likely(r->ind_e == NULL)) |
374 | 0 | r->ind_e = (OSSL_PARAM *)p; |
375 | 0 | } |
376 | 0 | break; |
377 | 0 | case 'n': |
378 | 0 | if (ossl_likely(strcmp("gine", s + 2) == 0)) { |
379 | 0 | if (ossl_likely(r->engine == NULL)) |
380 | 0 | r->engine = (OSSL_PARAM *)p; |
381 | 0 | } |
382 | 0 | } |
383 | 0 | break; |
384 | 0 | case 'k': |
385 | 0 | if (ossl_likely(strcmp("ey-check", s + 1) == 0)) { |
386 | 0 | if (ossl_likely(r->ind_k == NULL)) |
387 | 0 | r->ind_k = (OSSL_PARAM *)p; |
388 | 0 | } |
389 | 0 | break; |
390 | 0 | case 'p': |
391 | 0 | if (ossl_likely(strcmp("roperties", s + 1) == 0)) { |
392 | 0 | if (ossl_likely(r->propq == NULL)) |
393 | 0 | r->propq = (OSSL_PARAM *)p; |
394 | 0 | } |
395 | 0 | break; |
396 | 0 | case 's': |
397 | 0 | switch(s[1]) { |
398 | 0 | default: |
399 | 0 | break; |
400 | 0 | case 'e': |
401 | 0 | switch(s[2]) { |
402 | 0 | default: |
403 | 0 | break; |
404 | 0 | case 'c': |
405 | 0 | if (ossl_likely(strcmp("ret", s + 3) == 0)) { |
406 | 0 | if (ossl_likely(r->secret == NULL)) |
407 | 0 | r->secret = (OSSL_PARAM *)p; |
408 | 0 | } |
409 | 0 | break; |
410 | 0 | case 'e': |
411 | 0 | if (ossl_likely(strcmp("d", s + 3) == 0)) { |
412 | 0 | if (ossl_unlikely(r->num_seed >= TLSPRF_MAX_SEEDS)) { |
413 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_TOO_MANY_RECORDS, |
414 | 0 | "param %s present >%d times", s, TLSPRF_MAX_SEEDS); |
415 | 0 | return 0; |
416 | 0 | } |
417 | 0 | r->seed[r->num_seed++] = (OSSL_PARAM *)p; |
418 | 0 | } |
419 | 0 | } |
420 | 0 | } |
421 | 0 | } |
422 | 0 | return 1; |
423 | 0 | } |
424 | | #endif |
425 | | /* End of machine generated */ |
426 | | |
427 | | static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
428 | 0 | { |
429 | 0 | struct tls1prf_set_ctx_params_st p; |
430 | 0 | TLS1_PRF *ctx = vctx; |
431 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
432 | |
|
433 | 0 | if (ctx == NULL || !tls1prf_set_ctx_params_decoder(params, &p)) |
434 | 0 | return 0; |
435 | | |
436 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_e)) |
437 | 0 | return 0; |
438 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_d)) |
439 | 0 | return 0; |
440 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE2, p.ind_k)) |
441 | 0 | return 0; |
442 | | |
443 | 0 | if (p.digest != NULL) { |
444 | 0 | PROV_DIGEST digest; |
445 | 0 | const EVP_MD *md = NULL; |
446 | 0 | const char *dgst; |
447 | |
|
448 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(p.digest, &dgst)) |
449 | 0 | return 0; |
450 | | |
451 | 0 | if (OPENSSL_strcasecmp(dgst, OSSL_DIGEST_NAME_MD5_SHA1) == 0) { |
452 | 0 | if (!ossl_prov_macctx_load(&ctx->P_hash, NULL, NULL, NULL, |
453 | 0 | p.propq, p.engine, |
454 | 0 | OSSL_MAC_NAME_HMAC, NULL, |
455 | 0 | OSSL_DIGEST_NAME_MD5, libctx)) |
456 | 0 | return 0; |
457 | 0 | if (!ossl_prov_macctx_load(&ctx->P_sha1, NULL, NULL, NULL, |
458 | 0 | p.propq, p.engine, |
459 | 0 | OSSL_MAC_NAME_HMAC, NULL, |
460 | 0 | OSSL_DIGEST_NAME_SHA1, libctx)) |
461 | 0 | return 0; |
462 | 0 | } else { |
463 | 0 | EVP_MAC_CTX_free(ctx->P_sha1); |
464 | 0 | if (!ossl_prov_macctx_load(&ctx->P_hash, NULL, NULL, p.digest, |
465 | 0 | p.propq, p.engine, |
466 | 0 | OSSL_MAC_NAME_HMAC, NULL, NULL, libctx)) |
467 | 0 | return 0; |
468 | 0 | } |
469 | | |
470 | 0 | memset(&digest, 0, sizeof(digest)); |
471 | 0 | if (!ossl_prov_digest_load(&digest, p.digest, p.propq, p.engine, libctx)) |
472 | 0 | return 0; |
473 | | |
474 | 0 | md = ossl_prov_digest_md(&digest); |
475 | 0 | if (EVP_MD_xof(md)) { |
476 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
477 | 0 | ossl_prov_digest_reset(&digest); |
478 | 0 | return 0; |
479 | 0 | } |
480 | | |
481 | | #ifdef FIPS_MODULE |
482 | | if (!fips_digest_check_passed(ctx, md)) { |
483 | | ossl_prov_digest_reset(&digest); |
484 | | return 0; |
485 | | } |
486 | | #endif |
487 | | |
488 | 0 | ossl_prov_digest_reset(&digest); |
489 | 0 | } |
490 | | |
491 | 0 | if (p.secret != NULL) { |
492 | 0 | OPENSSL_clear_free(ctx->sec, ctx->seclen); |
493 | 0 | ctx->sec = NULL; |
494 | 0 | if (!OSSL_PARAM_get_octet_string(p.secret, (void **)&ctx->sec, 0, |
495 | 0 | &ctx->seclen)) |
496 | 0 | return 0; |
497 | |
|
498 | | #ifdef FIPS_MODULE |
499 | | if (!fips_key_check_passed(ctx)) |
500 | | return 0; |
501 | | #endif |
502 | 0 | } |
503 | | |
504 | | /* |
505 | | * The seed fields concatenate across set calls, so process them all |
506 | | * but only reallocate once. |
507 | | */ |
508 | 0 | if (p.num_seed > 0) { |
509 | 0 | const void *vals[TLSPRF_MAX_SEEDS]; |
510 | 0 | size_t sizes[TLSPRF_MAX_SEEDS]; |
511 | 0 | size_t seedlen = ctx->seedlen; |
512 | 0 | int i, n = 0; |
513 | |
|
514 | 0 | for (i = 0; i < p.num_seed; i++) { |
515 | 0 | sizes[i] = 0; |
516 | 0 | vals[i] = NULL; |
517 | 0 | if (p.seed[i]->data_size != 0 && p.seed[i]->data != NULL) { |
518 | 0 | int err = 0; |
519 | |
|
520 | 0 | if (!OSSL_PARAM_get_octet_string_ptr(p.seed[i], |
521 | 0 | vals + n, sizes + n)) |
522 | 0 | return 0; |
523 | | |
524 | 0 | seedlen = safe_add_size_t(seedlen, sizes[n], &err); |
525 | 0 | if (err) |
526 | 0 | return 0; |
527 | 0 | n++; |
528 | 0 | } |
529 | 0 | } |
530 | | |
531 | 0 | if (seedlen != ctx->seedlen) { |
532 | 0 | unsigned char *seed = OPENSSL_clear_realloc(ctx->seed, |
533 | 0 | ctx->seedlen, seedlen); |
534 | |
|
535 | 0 | if (seed == NULL) |
536 | 0 | return 0; |
537 | 0 | ctx->seed = seed; |
538 | | |
539 | | /* No errors are possible, so copy them across */ |
540 | 0 | for (i = 0; i < n; i++) { |
541 | 0 | memcpy(ctx->seed + ctx->seedlen, vals[i], sizes[i]); |
542 | 0 | ctx->seedlen += sizes[i]; |
543 | 0 | } |
544 | 0 | } |
545 | 0 | } |
546 | | |
547 | 0 | return 1; |
548 | 0 | } |
549 | | |
550 | | static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params( |
551 | | ossl_unused void *ctx, ossl_unused void *provctx) |
552 | 0 | { |
553 | 0 | return tls1prf_set_ctx_params_list; |
554 | 0 | } |
555 | | |
556 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
557 | | #ifndef tls1prf_get_ctx_params_list |
558 | | static const OSSL_PARAM tls1prf_get_ctx_params_list[] = { |
559 | | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
560 | | OSSL_PARAM_int(OSSL_KDF_PARAM_FIPS_APPROVED_INDICATOR, NULL), |
561 | | OSSL_PARAM_END |
562 | | }; |
563 | | #endif |
564 | | |
565 | | #ifndef tls1prf_get_ctx_params_st |
566 | | struct tls1prf_get_ctx_params_st { |
567 | | OSSL_PARAM *ind; |
568 | | OSSL_PARAM *size; |
569 | | }; |
570 | | #endif |
571 | | |
572 | | #ifndef tls1prf_get_ctx_params_decoder |
573 | | static int tls1prf_get_ctx_params_decoder |
574 | | (const OSSL_PARAM *p, struct tls1prf_get_ctx_params_st *r) |
575 | 0 | { |
576 | 0 | const char *s; |
577 | |
|
578 | 0 | memset(r, 0, sizeof(*r)); |
579 | 0 | if (p != NULL) |
580 | 0 | for (; (s = p->key) != NULL; p++) |
581 | 0 | switch(s[0]) { |
582 | 0 | default: |
583 | 0 | break; |
584 | 0 | case 'f': |
585 | 0 | if (ossl_likely(strcmp("ips-indicator", s + 1) == 0)) { |
586 | 0 | if (ossl_likely(r->ind == NULL)) |
587 | 0 | r->ind = (OSSL_PARAM *)p; |
588 | 0 | } |
589 | 0 | break; |
590 | 0 | case 's': |
591 | 0 | if (ossl_likely(strcmp("ize", s + 1) == 0)) { |
592 | 0 | if (ossl_likely(r->size == NULL)) |
593 | 0 | r->size = (OSSL_PARAM *)p; |
594 | 0 | } |
595 | 0 | } |
596 | 0 | return 1; |
597 | 0 | } |
598 | | #endif |
599 | | /* End of machine generated */ |
600 | | |
601 | | static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
602 | 0 | { |
603 | 0 | struct tls1prf_get_ctx_params_st p; |
604 | 0 | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
605 | |
|
606 | 0 | if (ctx == NULL || !tls1prf_get_ctx_params_decoder(params, &p)) |
607 | 0 | return 0; |
608 | | |
609 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
610 | 0 | return 0; |
611 | | |
612 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
613 | 0 | return 0; |
614 | 0 | return 1; |
615 | 0 | } |
616 | | |
617 | | static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params( |
618 | | ossl_unused void *ctx, ossl_unused void *provctx) |
619 | 0 | { |
620 | 0 | return tls1prf_get_ctx_params_list; |
621 | 0 | } |
622 | | |
623 | | const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = { |
624 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new }, |
625 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_tls1_prf_dup }, |
626 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free }, |
627 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset }, |
628 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive }, |
629 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
630 | | (void(*)(void))kdf_tls1_prf_settable_ctx_params }, |
631 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, |
632 | | (void(*)(void))kdf_tls1_prf_set_ctx_params }, |
633 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
634 | | (void(*)(void))kdf_tls1_prf_gettable_ctx_params }, |
635 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, |
636 | | (void(*)(void))kdf_tls1_prf_get_ctx_params }, |
637 | | OSSL_DISPATCH_END |
638 | | }; |
639 | | |
640 | | /* |
641 | | * Refer to "The TLS Protocol Version 1.0" Section 5 |
642 | | * (https://tools.ietf.org/html/rfc2246#section-5) and |
643 | | * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 |
644 | | * (https://tools.ietf.org/html/rfc5246#section-5). |
645 | | * |
646 | | * P_<hash> is an expansion function that uses a single hash function to expand |
647 | | * a secret and seed into an arbitrary quantity of output: |
648 | | * |
649 | | * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) + |
650 | | * HMAC_<hash>(secret, A(2) + seed) + |
651 | | * HMAC_<hash>(secret, A(3) + seed) + ... |
652 | | * |
653 | | * where + indicates concatenation. P_<hash> can be iterated as many times as |
654 | | * is necessary to produce the required quantity of data. |
655 | | * |
656 | | * A(i) is defined as: |
657 | | * A(0) = seed |
658 | | * A(i) = HMAC_<hash>(secret, A(i-1)) |
659 | | */ |
660 | | static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init, |
661 | | const unsigned char *sec, size_t sec_len, |
662 | | const unsigned char *seed, size_t seed_len, |
663 | | unsigned char *out, size_t olen) |
664 | 0 | { |
665 | 0 | size_t chunk; |
666 | 0 | EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL; |
667 | 0 | unsigned char Ai[EVP_MAX_MD_SIZE]; |
668 | 0 | size_t Ai_len; |
669 | 0 | int ret = 0; |
670 | |
|
671 | 0 | if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL)) |
672 | 0 | goto err; |
673 | 0 | chunk = EVP_MAC_CTX_get_mac_size(ctx_init); |
674 | 0 | if (chunk == 0) |
675 | 0 | goto err; |
676 | | /* A(0) = seed */ |
677 | 0 | ctx_Ai = EVP_MAC_CTX_dup(ctx_init); |
678 | 0 | if (ctx_Ai == NULL) |
679 | 0 | goto err; |
680 | 0 | if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len)) |
681 | 0 | goto err; |
682 | | |
683 | 0 | for (;;) { |
684 | | /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */ |
685 | 0 | if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai))) |
686 | 0 | goto err; |
687 | 0 | EVP_MAC_CTX_free(ctx_Ai); |
688 | 0 | ctx_Ai = NULL; |
689 | | |
690 | | /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */ |
691 | 0 | ctx = EVP_MAC_CTX_dup(ctx_init); |
692 | 0 | if (ctx == NULL) |
693 | 0 | goto err; |
694 | 0 | if (!EVP_MAC_update(ctx, Ai, Ai_len)) |
695 | 0 | goto err; |
696 | | /* save state for calculating next A(i) value */ |
697 | 0 | if (olen > chunk) { |
698 | 0 | ctx_Ai = EVP_MAC_CTX_dup(ctx); |
699 | 0 | if (ctx_Ai == NULL) |
700 | 0 | goto err; |
701 | 0 | } |
702 | 0 | if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len)) |
703 | 0 | goto err; |
704 | 0 | if (olen <= chunk) { |
705 | | /* last chunk - use Ai as temp bounce buffer */ |
706 | 0 | if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai))) |
707 | 0 | goto err; |
708 | 0 | memcpy(out, Ai, olen); |
709 | 0 | break; |
710 | 0 | } |
711 | 0 | if (!EVP_MAC_final(ctx, out, NULL, olen)) |
712 | 0 | goto err; |
713 | 0 | EVP_MAC_CTX_free(ctx); |
714 | 0 | ctx = NULL; |
715 | 0 | out += chunk; |
716 | 0 | olen -= chunk; |
717 | 0 | } |
718 | 0 | ret = 1; |
719 | 0 | err: |
720 | 0 | EVP_MAC_CTX_free(ctx); |
721 | 0 | EVP_MAC_CTX_free(ctx_Ai); |
722 | 0 | OPENSSL_cleanse(Ai, sizeof(Ai)); |
723 | 0 | return ret; |
724 | 0 | } |
725 | | |
726 | | /* |
727 | | * Refer to "The TLS Protocol Version 1.0" Section 5 |
728 | | * (https://tools.ietf.org/html/rfc2246#section-5) and |
729 | | * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 |
730 | | * (https://tools.ietf.org/html/rfc5246#section-5). |
731 | | * |
732 | | * For TLS v1.0 and TLS v1.1: |
733 | | * |
734 | | * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR |
735 | | * P_SHA-1(S2, label + seed) |
736 | | * |
737 | | * S1 is taken from the first half of the secret, S2 from the second half. |
738 | | * |
739 | | * L_S = length in bytes of secret; |
740 | | * L_S1 = L_S2 = ceil(L_S / 2); |
741 | | * |
742 | | * For TLS v1.2: |
743 | | * |
744 | | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
745 | | */ |
746 | | static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx, |
747 | | const unsigned char *sec, size_t slen, |
748 | | const unsigned char *seed, size_t seed_len, |
749 | | unsigned char *out, size_t olen) |
750 | 0 | { |
751 | 0 | if (sha1ctx != NULL) { |
752 | | /* TLS v1.0 and TLS v1.1 */ |
753 | 0 | size_t i; |
754 | 0 | unsigned char *tmp; |
755 | | /* calc: L_S1 = L_S2 = ceil(L_S / 2) */ |
756 | 0 | size_t L_S1 = (slen + 1) / 2; |
757 | 0 | size_t L_S2 = L_S1; |
758 | |
|
759 | 0 | if (!tls1_prf_P_hash(mdctx, sec, L_S1, |
760 | 0 | seed, seed_len, out, olen)) |
761 | 0 | return 0; |
762 | | |
763 | 0 | if ((tmp = OPENSSL_malloc(olen)) == NULL) |
764 | 0 | return 0; |
765 | | |
766 | 0 | if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2, |
767 | 0 | seed, seed_len, tmp, olen)) { |
768 | 0 | OPENSSL_clear_free(tmp, olen); |
769 | 0 | return 0; |
770 | 0 | } |
771 | 0 | for (i = 0; i < olen; i++) |
772 | 0 | out[i] ^= tmp[i]; |
773 | 0 | OPENSSL_clear_free(tmp, olen); |
774 | 0 | return 1; |
775 | 0 | } |
776 | | |
777 | | /* TLS v1.2 */ |
778 | 0 | if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen)) |
779 | 0 | return 0; |
780 | | |
781 | 0 | return 1; |
782 | 0 | } |