VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.5/providers/implementations/kdfs/tls1_prf.c@ 105132

最後變更 在這個檔案從105132是 104078,由 vboxsync 提交於 12 月 前

openssl-3.1.5: Applied and adjusted our OpenSSL changes to 3.1.4. bugref:10638

檔案大小: 15.3 KB
 
1/*
2 * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/*
11 * Refer to "The TLS Protocol Version 1.0" Section 5
12 * (https://tools.ietf.org/html/rfc2246#section-5) and
13 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
14 * (https://tools.ietf.org/html/rfc5246#section-5).
15 *
16 * For TLS v1.0 and TLS v1.1 the TLS PRF algorithm is given by:
17 *
18 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
19 * P_SHA-1(S2, label + seed)
20 *
21 * where P_MD5 and P_SHA-1 are defined by P_<hash>, below, and S1 and S2 are
22 * two halves of the secret (with the possibility of one shared byte, in the
23 * case where the length of the original secret is odd). S1 is taken from the
24 * first half of the secret, S2 from the second half.
25 *
26 * For TLS v1.2 the TLS PRF algorithm is given by:
27 *
28 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
29 *
30 * where hash is SHA-256 for all cipher suites defined in RFC 5246 as well as
31 * those published prior to TLS v1.2 while the TLS v1.2 protocol is in effect,
32 * unless defined otherwise by the cipher suite.
33 *
34 * P_<hash> is an expansion function that uses a single hash function to expand
35 * a secret and seed into an arbitrary quantity of output:
36 *
37 * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
38 * HMAC_<hash>(secret, A(2) + seed) +
39 * HMAC_<hash>(secret, A(3) + seed) + ...
40 *
41 * where + indicates concatenation. P_<hash> can be iterated as many times as
42 * is necessary to produce the required quantity of data.
43 *
44 * A(i) is defined as:
45 * A(0) = seed
46 * A(i) = HMAC_<hash>(secret, A(i-1))
47 */
48
49/*
50 * Low level APIs (such as DH) are deprecated for public use, but still ok for
51 * internal use.
52 */
53#include "internal/deprecated.h"
54
55#include <stdio.h>
56#include <stdarg.h>
57#include <string.h>
58#include <openssl/evp.h>
59#include <openssl/kdf.h>
60#include <openssl/core_names.h>
61#include <openssl/params.h>
62#include <openssl/proverr.h>
63#include "internal/cryptlib.h"
64#include "internal/numbers.h"
65#include "crypto/evp.h"
66#include "prov/provider_ctx.h"
67#include "prov/providercommon.h"
68#include "prov/implementations.h"
69#include "prov/provider_util.h"
70#include "prov/securitycheck.h"
71#include "internal/e_os.h"
72
73static OSSL_FUNC_kdf_newctx_fn kdf_tls1_prf_new;
74static OSSL_FUNC_kdf_dupctx_fn kdf_tls1_prf_dup;
75static OSSL_FUNC_kdf_freectx_fn kdf_tls1_prf_free;
76static OSSL_FUNC_kdf_reset_fn kdf_tls1_prf_reset;
77static OSSL_FUNC_kdf_derive_fn kdf_tls1_prf_derive;
78static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_prf_settable_ctx_params;
79static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_prf_set_ctx_params;
80static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_tls1_prf_gettable_ctx_params;
81static OSSL_FUNC_kdf_get_ctx_params_fn kdf_tls1_prf_get_ctx_params;
82
83static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
84 const unsigned char *sec, size_t slen,
85 const unsigned char *seed, size_t seed_len,
86 unsigned char *out, size_t olen);
87
88#define TLS1_PRF_MAXBUF 1024
89#define TLS_MD_MASTER_SECRET_CONST "\x6d\x61\x73\x74\x65\x72\x20\x73\x65\x63\x72\x65\x74"
90#define TLS_MD_MASTER_SECRET_CONST_SIZE 13
91
92/* TLS KDF kdf context structure */
93typedef struct {
94 void *provctx;
95
96 /* MAC context for the main digest */
97 EVP_MAC_CTX *P_hash;
98 /* MAC context for SHA1 for the MD5/SHA-1 combined PRF */
99 EVP_MAC_CTX *P_sha1;
100
101 /* Secret value to use for PRF */
102 unsigned char *sec;
103 size_t seclen;
104 /* Buffer of concatenated seed data */
105 unsigned char seed[TLS1_PRF_MAXBUF];
106 size_t seedlen;
107} TLS1_PRF;
108
109static void *kdf_tls1_prf_new(void *provctx)
110{
111 TLS1_PRF *ctx;
112
113 if (!ossl_prov_is_running())
114 return NULL;
115
116 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
117 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
118 return NULL;
119 }
120 ctx->provctx = provctx;
121 return ctx;
122}
123
124static void kdf_tls1_prf_free(void *vctx)
125{
126 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
127
128 if (ctx != NULL) {
129 kdf_tls1_prf_reset(ctx);
130 OPENSSL_free(ctx);
131 }
132}
133
134static void kdf_tls1_prf_reset(void *vctx)
135{
136 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
137 void *provctx = ctx->provctx;
138
139 EVP_MAC_CTX_free(ctx->P_hash);
140 EVP_MAC_CTX_free(ctx->P_sha1);
141 OPENSSL_clear_free(ctx->sec, ctx->seclen);
142 OPENSSL_cleanse(ctx->seed, ctx->seedlen);
143 memset(ctx, 0, sizeof(*ctx));
144 ctx->provctx = provctx;
145}
146
147static void *kdf_tls1_prf_dup(void *vctx)
148{
149 const TLS1_PRF *src = (const TLS1_PRF *)vctx;
150 TLS1_PRF *dest;
151
152 dest = kdf_tls1_prf_new(src->provctx);
153 if (dest != NULL) {
154 if (src->P_hash != NULL
155 && (dest->P_hash = EVP_MAC_CTX_dup(src->P_hash)) == NULL)
156 goto err;
157 if (src->P_sha1 != NULL
158 && (dest->P_sha1 = EVP_MAC_CTX_dup(src->P_sha1)) == NULL)
159 goto err;
160 if (!ossl_prov_memdup(src->sec, src->seclen, &dest->sec, &dest->seclen))
161 goto err;
162 memcpy(dest->seed, src->seed, src->seedlen);
163 dest->seedlen = src->seedlen;
164 }
165 return dest;
166
167 err:
168 kdf_tls1_prf_free(dest);
169 return NULL;
170}
171
172static int kdf_tls1_prf_derive(void *vctx, unsigned char *key, size_t keylen,
173 const OSSL_PARAM params[])
174{
175 TLS1_PRF *ctx = (TLS1_PRF *)vctx;
176 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
177
178 if (!ossl_prov_is_running() || !kdf_tls1_prf_set_ctx_params(ctx, params))
179 return 0;
180
181 if (ctx->P_hash == NULL) {
182 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
183 return 0;
184 }
185 if (ctx->sec == NULL) {
186 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET);
187 return 0;
188 }
189 if (ctx->seedlen == 0) {
190 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SEED);
191 return 0;
192 }
193 if (keylen == 0) {
194 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
195 return 0;
196 }
197
198 /*
199 * The seed buffer is prepended with a label.
200 * If EMS mode is enforced then the label "master secret" is not allowed,
201 * We do the check this way since the PRF is used for other purposes, as well
202 * as "extended master secret".
203 */
204 if (ossl_tls1_prf_ems_check_enabled(libctx)) {
205 if (ctx->seedlen >= TLS_MD_MASTER_SECRET_CONST_SIZE
206 && memcmp(ctx->seed, TLS_MD_MASTER_SECRET_CONST,
207 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0) {
208 ERR_raise(ERR_LIB_PROV, PROV_R_EMS_NOT_ENABLED);
209 return 0;
210 }
211 }
212
213 return tls1_prf_alg(ctx->P_hash, ctx->P_sha1,
214 ctx->sec, ctx->seclen,
215 ctx->seed, ctx->seedlen,
216 key, keylen);
217}
218
219static int kdf_tls1_prf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
220{
221 const OSSL_PARAM *p;
222 TLS1_PRF *ctx = vctx;
223 OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
224
225 if (params == NULL)
226 return 1;
227
228 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DIGEST)) != NULL) {
229 if (OPENSSL_strcasecmp(p->data, SN_md5_sha1) == 0) {
230 if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
231 OSSL_MAC_NAME_HMAC,
232 NULL, SN_md5, libctx)
233 || !ossl_prov_macctx_load_from_params(&ctx->P_sha1, params,
234 OSSL_MAC_NAME_HMAC,
235 NULL, SN_sha1, libctx))
236 return 0;
237 } else {
238 EVP_MAC_CTX_free(ctx->P_sha1);
239 if (!ossl_prov_macctx_load_from_params(&ctx->P_hash, params,
240 OSSL_MAC_NAME_HMAC,
241 NULL, NULL, libctx))
242 return 0;
243 }
244 }
245
246 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL) {
247 OPENSSL_clear_free(ctx->sec, ctx->seclen);
248 ctx->sec = NULL;
249 if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->sec, 0, &ctx->seclen))
250 return 0;
251 }
252 /* The seed fields concatenate, so process them all */
253 if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SEED)) != NULL) {
254 for (; p != NULL; p = OSSL_PARAM_locate_const(p + 1,
255 OSSL_KDF_PARAM_SEED)) {
256 const void *q = ctx->seed + ctx->seedlen;
257 size_t sz = 0;
258
259 if (p->data_size != 0
260 && p->data != NULL
261 && !OSSL_PARAM_get_octet_string(p, (void **)&q,
262 TLS1_PRF_MAXBUF - ctx->seedlen,
263 &sz))
264 return 0;
265 ctx->seedlen += sz;
266 }
267 }
268 return 1;
269}
270
271static const OSSL_PARAM *kdf_tls1_prf_settable_ctx_params(
272 ossl_unused void *ctx, ossl_unused void *provctx)
273{
274 static const OSSL_PARAM known_settable_ctx_params[] = {
275 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0),
276 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0),
277 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0),
278 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SEED, NULL, 0),
279 OSSL_PARAM_END
280 };
281 return known_settable_ctx_params;
282}
283
284static int kdf_tls1_prf_get_ctx_params(void *vctx, OSSL_PARAM params[])
285{
286 OSSL_PARAM *p;
287
288 if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL)
289 return OSSL_PARAM_set_size_t(p, SIZE_MAX);
290 return -2;
291}
292
293static const OSSL_PARAM *kdf_tls1_prf_gettable_ctx_params(
294 ossl_unused void *ctx, ossl_unused void *provctx)
295{
296 static const OSSL_PARAM known_gettable_ctx_params[] = {
297 OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL),
298 OSSL_PARAM_END
299 };
300 return known_gettable_ctx_params;
301}
302
303const OSSL_DISPATCH ossl_kdf_tls1_prf_functions[] = {
304 { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_tls1_prf_new },
305 { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_tls1_prf_dup },
306 { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_tls1_prf_free },
307 { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_tls1_prf_reset },
308 { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_prf_derive },
309 { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS,
310 (void(*)(void))kdf_tls1_prf_settable_ctx_params },
311 { OSSL_FUNC_KDF_SET_CTX_PARAMS,
312 (void(*)(void))kdf_tls1_prf_set_ctx_params },
313 { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
314 (void(*)(void))kdf_tls1_prf_gettable_ctx_params },
315 { OSSL_FUNC_KDF_GET_CTX_PARAMS,
316 (void(*)(void))kdf_tls1_prf_get_ctx_params },
317 { 0, NULL }
318};
319
320/*
321 * Refer to "The TLS Protocol Version 1.0" Section 5
322 * (https://tools.ietf.org/html/rfc2246#section-5) and
323 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
324 * (https://tools.ietf.org/html/rfc5246#section-5).
325 *
326 * P_<hash> is an expansion function that uses a single hash function to expand
327 * a secret and seed into an arbitrary quantity of output:
328 *
329 * P_<hash>(secret, seed) = HMAC_<hash>(secret, A(1) + seed) +
330 * HMAC_<hash>(secret, A(2) + seed) +
331 * HMAC_<hash>(secret, A(3) + seed) + ...
332 *
333 * where + indicates concatenation. P_<hash> can be iterated as many times as
334 * is necessary to produce the required quantity of data.
335 *
336 * A(i) is defined as:
337 * A(0) = seed
338 * A(i) = HMAC_<hash>(secret, A(i-1))
339 */
340static int tls1_prf_P_hash(EVP_MAC_CTX *ctx_init,
341 const unsigned char *sec, size_t sec_len,
342 const unsigned char *seed, size_t seed_len,
343 unsigned char *out, size_t olen)
344{
345 size_t chunk;
346 EVP_MAC_CTX *ctx = NULL, *ctx_Ai = NULL;
347 unsigned char Ai[EVP_MAX_MD_SIZE];
348 size_t Ai_len;
349 int ret = 0;
350
351 if (!EVP_MAC_init(ctx_init, sec, sec_len, NULL))
352 goto err;
353 chunk = EVP_MAC_CTX_get_mac_size(ctx_init);
354 if (chunk == 0)
355 goto err;
356 /* A(0) = seed */
357 ctx_Ai = EVP_MAC_CTX_dup(ctx_init);
358 if (ctx_Ai == NULL)
359 goto err;
360 if (seed != NULL && !EVP_MAC_update(ctx_Ai, seed, seed_len))
361 goto err;
362
363 for (;;) {
364 /* calc: A(i) = HMAC_<hash>(secret, A(i-1)) */
365 if (!EVP_MAC_final(ctx_Ai, Ai, &Ai_len, sizeof(Ai)))
366 goto err;
367 EVP_MAC_CTX_free(ctx_Ai);
368 ctx_Ai = NULL;
369
370 /* calc next chunk: HMAC_<hash>(secret, A(i) + seed) */
371 ctx = EVP_MAC_CTX_dup(ctx_init);
372 if (ctx == NULL)
373 goto err;
374 if (!EVP_MAC_update(ctx, Ai, Ai_len))
375 goto err;
376 /* save state for calculating next A(i) value */
377 if (olen > chunk) {
378 ctx_Ai = EVP_MAC_CTX_dup(ctx);
379 if (ctx_Ai == NULL)
380 goto err;
381 }
382 if (seed != NULL && !EVP_MAC_update(ctx, seed, seed_len))
383 goto err;
384 if (olen <= chunk) {
385 /* last chunk - use Ai as temp bounce buffer */
386 if (!EVP_MAC_final(ctx, Ai, &Ai_len, sizeof(Ai)))
387 goto err;
388 memcpy(out, Ai, olen);
389 break;
390 }
391 if (!EVP_MAC_final(ctx, out, NULL, olen))
392 goto err;
393 EVP_MAC_CTX_free(ctx);
394 ctx = NULL;
395 out += chunk;
396 olen -= chunk;
397 }
398 ret = 1;
399 err:
400 EVP_MAC_CTX_free(ctx);
401 EVP_MAC_CTX_free(ctx_Ai);
402 OPENSSL_cleanse(Ai, sizeof(Ai));
403 return ret;
404}
405
406/*
407 * Refer to "The TLS Protocol Version 1.0" Section 5
408 * (https://tools.ietf.org/html/rfc2246#section-5) and
409 * "The Transport Layer Security (TLS) Protocol Version 1.2" Section 5
410 * (https://tools.ietf.org/html/rfc5246#section-5).
411 *
412 * For TLS v1.0 and TLS v1.1:
413 *
414 * PRF(secret, label, seed) = P_MD5(S1, label + seed) XOR
415 * P_SHA-1(S2, label + seed)
416 *
417 * S1 is taken from the first half of the secret, S2 from the second half.
418 *
419 * L_S = length in bytes of secret;
420 * L_S1 = L_S2 = ceil(L_S / 2);
421 *
422 * For TLS v1.2:
423 *
424 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
425 */
426static int tls1_prf_alg(EVP_MAC_CTX *mdctx, EVP_MAC_CTX *sha1ctx,
427 const unsigned char *sec, size_t slen,
428 const unsigned char *seed, size_t seed_len,
429 unsigned char *out, size_t olen)
430{
431 if (sha1ctx != NULL) {
432 /* TLS v1.0 and TLS v1.1 */
433 size_t i;
434 unsigned char *tmp;
435 /* calc: L_S1 = L_S2 = ceil(L_S / 2) */
436 size_t L_S1 = (slen + 1) / 2;
437 size_t L_S2 = L_S1;
438
439 if (!tls1_prf_P_hash(mdctx, sec, L_S1,
440 seed, seed_len, out, olen))
441 return 0;
442
443 if ((tmp = OPENSSL_malloc(olen)) == NULL) {
444 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
445 return 0;
446 }
447
448 if (!tls1_prf_P_hash(sha1ctx, sec + slen - L_S2, L_S2,
449 seed, seed_len, tmp, olen)) {
450 OPENSSL_clear_free(tmp, olen);
451 return 0;
452 }
453 for (i = 0; i < olen; i++)
454 out[i] ^= tmp[i];
455 OPENSSL_clear_free(tmp, olen);
456 return 1;
457 }
458
459 /* TLS v1.2 */
460 if (!tls1_prf_P_hash(mdctx, sec, slen, seed, seed_len, out, olen))
461 return 0;
462
463 return 1;
464}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette