/src/openssl30/providers/implementations/kdfs/tls1_prf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2022 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 | | #include <stdio.h> |
49 | | #include <stdarg.h> |
50 | | #include <string.h> |
51 | | #include <openssl/evp.h> |
52 | | #include <openssl/kdf.h> |
53 | | #include <openssl/core_names.h> |
54 | | #include <openssl/params.h> |
55 | | #include <openssl/proverr.h> |
56 | | #include "internal/cryptlib.h" |
57 | | #include "internal/numbers.h" |
58 | | #include "crypto/evp.h" |
59 | | #include "prov/provider_ctx.h" |
60 | | #include "prov/providercommon.h" |
61 | | #include "prov/implementations.h" |
62 | | #include "prov/provider_util.h" |
63 | | #include "e_os.h" |
64 | | |
65 | | static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new; |
66 | | static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free; |
67 | | static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset; |
68 | | static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive; |
69 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params; |
70 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params; |
71 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params; |
72 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params; |
73 | | |
74 | | static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx, |
75 | | const unsigned char *sec, size_t slen, |
76 | | const unsigned char *seed, size_t seed_len, |
77 | | unsigned char *out, size_t olen); |
78 | | |
79 | 44.4k | #define TLS1_PRF_MAXBUF 1024 |
80 | | |
81 | | /* TLS KDF kdf context structure */ |
82 | | typedef struct { |
83 | | void *provctx; |
84 | | |
85 | | /* MAC context for the main digest */ |
86 | | EVP_MAC_CTX *P_hash; |
87 | | /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */ |
88 | | EVP_MAC_CTX *P_sha1; |
89 | | |
90 | | /* Secret value to use for PRF */ |
91 | | unsigned char *sec; |
92 | | size_t seclen; |
93 | | /* Buffer of concatenated seed data */ |
94 | | unsigned char seed[TLS1_PRF_MAXBUF]; |
95 | | size_t seedlen; |
96 | | } TLS1_PRF; |
97 | | |
98 | | static void *kdf_tls1_prf_new(void *provctx) |
99 | 9.38k | { |
100 | 9.38k | TLS1_PRF *ctx; |
101 | | |
102 | 9.38k | if (!ossl_prov_is_running()) |
103 | 0 | return NULL; |
104 | | |
105 | 9.38k | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) { |
106 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
107 | 0 | return NULL; |
108 | 0 | } |
109 | 9.38k | ctx->provctx = provctx; |
110 | 9.38k | return ctx; |
111 | 9.38k | } |
112 | | |
113 | | static void kdf_tls1_prf_free(void *vctx) |
114 | 28.6k | { |
115 | 28.6k | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
116 | | |
117 | 28.6k | if (ctx != NULL) { |
118 | 28.6k | kdf_tls1_prf_reset(ctx); |
119 | 28.6k | OPENSSL_free(ctx); |
120 | 28.6k | } |
121 | 28.6k | } |
122 | | |
123 | | static void kdf_tls1_prf_reset(void *vctx) |
124 | 28.6k | { |
125 | 28.6k | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
126 | 28.6k | void *provctx = ctx->provctx; |
127 | | |
128 | 28.6k | EVP_MAC_CTX_free(ctx->P_hash); |
129 | 28.6k | EVP_MAC_CTX_free(ctx->P_sha1); |
130 | 28.6k | OPENSSL_clear_free(ctx->sec, ctx->seclen); |
131 | 28.6k | OPENSSL_cleanse(ctx->seed, ctx->seedlen); |
132 | 28.6k | memset(ctx, 0, sizeof(*ctx)); |
133 | 28.6k | ctx->provctx = provctx; |
134 | 28.6k | } |
135 | | |
136 | | static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen, |
137 | | const OSSL_PARAM params[]) |
138 | 15.6k | { |
139 | 15.6k | TLS1_PRF *ctx = (TLS1_PRF *)vctx; |
140 | | |
141 | 15.6k | if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params)) |
142 | 0 | return 0; |
143 | | |
144 | 15.6k | if (ctx->P_hash == NULL) { |
145 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
146 | 0 | return 0; |
147 | 0 | } |
148 | 15.6k | if (ctx->sec == NULL) { |
149 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); |
150 | 0 | return 0; |
151 | 0 | } |
152 | 15.6k | if (ctx->seedlen == 0) { |
153 | 240 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED); |
154 | 240 | return 0; |
155 | 240 | } |
156 | 15.4k | if (keylen == 0) { |
157 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
158 | 0 | return 0; |
159 | 0 | } |
160 | | |
161 | 15.4k | return tls1_prf_alg(ctx->P_hash, ctx->P_sha1, |
162 | 15.4k | ctx->sec, ctx->seclen, |
163 | 15.4k | ctx->seed, ctx->seedlen, |
164 | 15.4k | key, keylen); |
165 | 15.4k | } |
166 | | |
167 | | static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
168 | 16.6k | { |
169 | 16.6k | const OSSL_PARAM *p; |
170 | 16.6k | TLS1_PRF *ctx = vctx; |
171 | 16.6k | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
172 | | |
173 | 16.6k | if (params == NULL) |
174 | 0 | return 1; |
175 | | |
176 | 16.6k | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) { |
177 | 16.6k | if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) { |
178 | 4.27k | if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params, |
179 | 4.27k | OSSL_MAC_NAME_HMAC, |
180 | 4.27k | NULL, SN_md5, libctx) |
181 | 4.27k | || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params, |
182 | 4.27k | OSSL_MAC_NAME_HMAC, |
183 | 4.27k | NULL, SN_sha1, libctx)) |
184 | 0 | return 0; |
185 | 12.3k | } else { |
186 | 12.3k | EVP_MAC_CTX_free(ctx->P_sha1); |
187 | 12.3k | if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params, |
188 | 12.3k | OSSL_MAC_NAME_HMAC, |
189 | 12.3k | NULL, NULL, libctx)) |
190 | 0 | return 0; |
191 | 12.3k | } |
192 | 16.6k | } |
193 | | |
194 | 16.6k | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) { |
195 | 16.6k | OPENSSL_clear_free(ctx->sec, ctx->seclen); |
196 | 16.6k | ctx->sec = NULL; |
197 | 16.6k | if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen)) |
198 | 0 | return 0; |
199 | 16.6k | } |
200 | | /* The seed fields concatenate, so process them all */ |
201 | 16.6k | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) { |
202 | 100k | for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1, |
203 | 83.3k | OSSL_KDF_PARAM_SEED)) { |
204 | 83.3k | const void *q = ctx->seed + ctx->seedlen; |
205 | 83.3k | size_t sz = 0; |
206 | | |
207 | 83.3k | if (p->data_size != 0 |
208 | 83.3k | && p->data != NULL |
209 | 83.3k | && !OSSL_PARAM_get_octet_string(p, (void **)&q, |
210 | 44.4k | TLS1_PRF_MAXBUF - ctx->seedlen, |
211 | 44.4k | &sz)) |
212 | 0 | return 0; |
213 | 83.3k | ctx->seedlen += sz; |
214 | 83.3k | } |
215 | 16.6k | } |
216 | 16.6k | return 1; |
217 | 16.6k | } |
218 | | |
219 | | static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params( |
220 | | ossl_unused void *ctx, ossl_unused void *provctx) |
221 | 1.16k | { |
222 | 1.16k | static const OSSL_PARAM known_settable_ctx_params[] = { |
223 | 1.16k | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
224 | 1.16k | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), |
225 | 1.16k | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0), |
226 | 1.16k | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0), |
227 | 1.16k | OSSL_PARAM_END |
228 | 1.16k | }; |
229 | 1.16k | return known_settable_ctx_params; |
230 | 1.16k | } |
231 | | |
232 | | static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
233 | 0 | { |
234 | 0 | OSSL_PARAM *p; |
235 | |
|
236 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) |
237 | 0 | return OSSL_PARAM_set_size_t(p, SIZE_MAX); |
238 | 0 | return -2; |
239 | 0 | } |
240 | | |
241 | | static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params( |
242 | | ossl_unused void *ctx, ossl_unused void *provctx) |
243 | 0 | { |
244 | 0 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
245 | 0 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
246 | 0 | OSSL_PARAM_END |
247 | 0 | }; |
248 | 0 | return known_gettable_ctx_params; |
249 | 0 | } |
250 | | |
251 | | const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = { |
252 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new }, |
253 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free }, |
254 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset }, |
255 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive }, |
256 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
257 | | (void(*)(void))kdf_tls1_prf_settable_ctx_params }, |
258 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, |
259 | | (void(*)(void))kdf_tls1_prf_set_ctx_params }, |
260 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
261 | | (void(*)(void))kdf_tls1_prf_gettable_ctx_params }, |
262 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, |
263 | | (void(*)(void))kdf_tls1_prf_get_ctx_params }, |
264 | | { 0, NULL } |
265 | | }; |
266 | | |
267 | | /* |
268 | | * Refer to "The TLS Protocol Version 1.0" Section 5 |
269 | | * (https://tools.ietf.org/html/rfc2246#section-5) and |
270 | | * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 |
271 | | * (https://tools.ietf.org/html/rfc5246#section-5). |
272 | | * |
273 | | * P_<hash> is an expansion function that uses a single hash function to expand |
274 | | * a secret and seed into an arbitrary quantity of output: |
275 | | * |
276 | | * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) + |
277 | | * HMAC_<hash>(secret, A(2) + seed) + |
278 | | * HMAC_<hash>(secret, A(3) + seed) + ... |
279 | | * |
280 | | * where + indicates concatenation. P_<hash> can be iterated as many times as |
281 | | * is necessary to produce the required quantity of data. |
282 | | * |
283 | | * A(i) is defined as: |
284 | | * A(0) = seed |
285 | | * A(i) = HMAC_<hash>(secret, A(i-1)) |
286 | | */ |
287 | | static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init, |
288 | | const unsigned char *sec, size_t sec_len, |
289 | | const unsigned char *seed, size_t seed_len, |
290 | | unsigned char *out, size_t olen) |
291 | 34.9k | { |
292 | 34.9k | size_t chunk; |
293 | 34.9k | EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL; |
294 | 34.9k | unsigned char Ai[EVP_MAX_MD_SIZE]; |
295 | 34.9k | size_t Ai_len; |
296 | 34.9k | int ret = 0; |
297 | | |
298 | 34.9k | if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL)) |
299 | 0 | goto err; |
300 | 34.9k | chunk = EVP_MAC_CTX_get_mac_size(ctx_init); |
301 | 34.9k | if (chunk == 0) |
302 | 2 | goto err; |
303 | | /* A(0) = seed */ |
304 | 34.8k | ctx_Ai = EVP_MAC_CTX_dup(ctx_init); |
305 | 34.8k | if (ctx_Ai == NULL) |
306 | 0 | goto err; |
307 | 34.8k | if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len)) |
308 | 0 | goto err; |
309 | | |
310 | 86.0k | for (;;) { |
311 | | /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */ |
312 | 86.0k | if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai))) |
313 | 0 | goto err; |
314 | 86.0k | EVP_MAC_CTX_free(ctx_Ai); |
315 | 86.0k | ctx_Ai = NULL; |
316 | | |
317 | | /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */ |
318 | 86.0k | ctx = EVP_MAC_CTX_dup(ctx_init); |
319 | 86.0k | if (ctx == NULL) |
320 | 0 | goto err; |
321 | 86.0k | if (!EVP_MAC_update(ctx, Ai, Ai_len)) |
322 | 0 | goto err; |
323 | | /* save state for calculating next A(i) value */ |
324 | 86.0k | if (olen > chunk) { |
325 | 51.1k | ctx_Ai = EVP_MAC_CTX_dup(ctx); |
326 | 51.1k | if (ctx_Ai == NULL) |
327 | 0 | goto err; |
328 | 51.1k | } |
329 | 86.0k | if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len)) |
330 | 0 | goto err; |
331 | 86.0k | if (olen <= chunk) { |
332 | | /* last chunk - use Ai as temp bounce buffer */ |
333 | 34.8k | if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai))) |
334 | 0 | goto err; |
335 | 34.8k | memcpy(out, Ai, olen); |
336 | 34.8k | break; |
337 | 34.8k | } |
338 | 51.1k | if (!EVP_MAC_final(ctx, out, NULL, olen)) |
339 | 0 | goto err; |
340 | 51.1k | EVP_MAC_CTX_free(ctx); |
341 | 51.1k | ctx = NULL; |
342 | 51.1k | out += chunk; |
343 | 51.1k | olen -= chunk; |
344 | 51.1k | } |
345 | 34.8k | ret = 1; |
346 | 34.9k | err: |
347 | 34.9k | EVP_MAC_CTX_free(ctx); |
348 | 34.9k | EVP_MAC_CTX_free(ctx_Ai); |
349 | 34.9k | OPENSSL_cleanse(Ai, sizeof(Ai)); |
350 | 34.9k | return ret; |
351 | 34.8k | } |
352 | | |
353 | | /* |
354 | | * Refer to "The TLS Protocol Version 1.0" Section 5 |
355 | | * (https://tools.ietf.org/html/rfc2246#section-5) and |
356 | | * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5 |
357 | | * (https://tools.ietf.org/html/rfc5246#section-5). |
358 | | * |
359 | | * For TLS v1.0 and TLS v1.1: |
360 | | * |
361 | | * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR |
362 | | * P_SHA-1(S2, label + seed) |
363 | | * |
364 | | * S1 is taken from the first half of the secret, S2 from the second half. |
365 | | * |
366 | | * L_S = length in bytes of secret; |
367 | | * L_S1 = L_S2 = ceil(L_S / 2); |
368 | | * |
369 | | * For TLS v1.2: |
370 | | * |
371 | | * PRF(secret, label, seed) = P_<hash>(secret, label + seed) |
372 | | */ |
373 | | static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx, |
374 | | const unsigned char *sec, size_t slen, |
375 | | const unsigned char *seed, size_t seed_len, |
376 | | unsigned char *out, size_t olen) |
377 | 27.7k | { |
378 | 27.7k | if (sha1ctx != NULL) { |
379 | | /* TLS v1.0 and TLS v1.1 */ |
380 | 7.13k | size_t i; |
381 | 7.13k | unsigned char *tmp; |
382 | | /* calc: L_S1 = L_S2 = ceil(L_S / 2) */ |
383 | 7.13k | size_t L_S1 = (slen + 1) / 2; |
384 | 7.13k | size_t L_S2 = L_S1; |
385 | | |
386 | 7.13k | if (!tls1_prf_P_hash(mdctx, sec, L_S1, |
387 | 7.13k | seed, seed_len, out, olen)) |
388 | 0 | return 0; |
389 | | |
390 | 7.13k | if ((tmp = OPENSSL_malloc(olen)) == NULL) { |
391 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
392 | 0 | return 0; |
393 | 0 | } |
394 | | |
395 | 7.13k | if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2, |
396 | 7.13k | seed, seed_len, tmp, olen)) { |
397 | 0 | OPENSSL_clear_free(tmp, olen); |
398 | 0 | return 0; |
399 | 0 | } |
400 | 374k | for (i = 0; i < olen; i++) |
401 | 367k | out[i] ^= tmp[i]; |
402 | 7.13k | OPENSSL_clear_free(tmp, olen); |
403 | 7.13k | return 1; |
404 | 7.13k | } |
405 | | |
406 | | /* TLS v1.2 */ |
407 | 20.6k | if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen)) |
408 | 2 | return 0; |
409 | | |
410 | 20.6k | return 1; |
411 | 20.6k | } |