/src/openssl31/providers/implementations/kdfs/pkcs12kdf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-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 | | #include <openssl/trace.h> |
11 | | #include <stdlib.h> |
12 | | #include <stdarg.h> |
13 | | #include <string.h> |
14 | | #include <openssl/evp.h> |
15 | | #include <openssl/kdf.h> |
16 | | #include <openssl/core_names.h> |
17 | | #include <openssl/proverr.h> |
18 | | #include "internal/cryptlib.h" |
19 | | #include "internal/numbers.h" |
20 | | #include "crypto/evp.h" |
21 | | #include "prov/provider_ctx.h" |
22 | | #include "prov/providercommon.h" |
23 | | #include "prov/implementations.h" |
24 | | #include "prov/provider_util.h" |
25 | | |
26 | | static OSSL_FUNC_kdf_newctx_fn kdf_pkcs12_new; |
27 | | static OSSL_FUNC_kdf_dupctx_fn kdf_pkcs12_dup; |
28 | | static OSSL_FUNC_kdf_freectx_fn kdf_pkcs12_free; |
29 | | static OSSL_FUNC_kdf_reset_fn kdf_pkcs12_reset; |
30 | | static OSSL_FUNC_kdf_derive_fn kdf_pkcs12_derive; |
31 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pkcs12_settable_ctx_params; |
32 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pkcs12_set_ctx_params; |
33 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pkcs12_gettable_ctx_params; |
34 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pkcs12_get_ctx_params; |
35 | | |
36 | | typedef struct { |
37 | | void *provctx; |
38 | | PROV_DIGEST digest; |
39 | | unsigned char *pass; |
40 | | size_t pass_len; |
41 | | unsigned char *salt; |
42 | | size_t salt_len; |
43 | | uint64_t iter; |
44 | | int id; |
45 | | } KDF_PKCS12; |
46 | | |
47 | | /* PKCS12 compatible key/IV generation */ |
48 | | |
49 | | static int pkcs12kdf_derive(const unsigned char *pass, size_t passlen, |
50 | | const unsigned char *salt, size_t saltlen, |
51 | | int id, uint64_t iter, const EVP_MD *md_type, |
52 | | unsigned char *out, size_t n) |
53 | 141 | { |
54 | 141 | unsigned char *B = NULL, *D = NULL, *I = NULL, *p = NULL, *Ai = NULL; |
55 | 141 | size_t Slen, Plen, Ilen; |
56 | 141 | size_t i, j, k, u, v; |
57 | 141 | uint64_t iter_cnt; |
58 | 141 | int ret = 0, ui, vi; |
59 | 141 | EVP_MD_CTX *ctx = NULL; |
60 | | |
61 | 141 | ctx = EVP_MD_CTX_new(); |
62 | 141 | if (ctx == NULL) { |
63 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
64 | 0 | goto end; |
65 | 0 | } |
66 | 141 | vi = EVP_MD_get_block_size(md_type); |
67 | 141 | ui = EVP_MD_get_size(md_type); |
68 | 141 | if (ui <= 0 || vi <= 0) { |
69 | 1 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST_SIZE); |
70 | 1 | goto end; |
71 | 1 | } |
72 | 140 | u = (size_t)ui; |
73 | 140 | v = (size_t)vi; |
74 | 140 | D = OPENSSL_malloc(v); |
75 | 140 | Ai = OPENSSL_malloc(u); |
76 | 140 | B = OPENSSL_malloc(v + 1); |
77 | 140 | Slen = v * ((saltlen + v - 1) / v); |
78 | 140 | if (passlen != 0) |
79 | 124 | Plen = v * ((passlen + v - 1) / v); |
80 | 16 | else |
81 | 16 | Plen = 0; |
82 | 140 | Ilen = Slen + Plen; |
83 | 140 | I = OPENSSL_malloc(Ilen); |
84 | 140 | if (D == NULL || Ai == NULL || B == NULL || I == NULL) { |
85 | 2 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
86 | 2 | goto end; |
87 | 2 | } |
88 | 13.2k | for (i = 0; i < v; i++) |
89 | 13.1k | D[i] = id; |
90 | 138 | p = I; |
91 | 5.21M | for (i = 0; i < Slen; i++) |
92 | 5.21M | *p++ = salt[i % saltlen]; |
93 | 2.34M | for (i = 0; i < Plen; i++) |
94 | 2.34M | *p++ = pass[i % passlen]; |
95 | 229 | for (;;) { |
96 | 229 | if (!EVP_DigestInit_ex(ctx, md_type, NULL) |
97 | 229 | || !EVP_DigestUpdate(ctx, D, v) |
98 | 229 | || !EVP_DigestUpdate(ctx, I, Ilen) |
99 | 229 | || !EVP_DigestFinal_ex(ctx, Ai, NULL)) |
100 | 0 | goto end; |
101 | 229 | for (iter_cnt = 1; iter_cnt < iter; iter_cnt++) { |
102 | 0 | if (!EVP_DigestInit_ex(ctx, md_type, NULL) |
103 | 0 | || !EVP_DigestUpdate(ctx, Ai, u) |
104 | 0 | || !EVP_DigestFinal_ex(ctx, Ai, NULL)) |
105 | 0 | goto end; |
106 | 0 | } |
107 | 229 | memcpy(out, Ai, n < u ? n : u); |
108 | 229 | if (u >= n) { |
109 | 138 | ret = 1; |
110 | 138 | break; |
111 | 138 | } |
112 | 91 | n -= u; |
113 | 91 | out += u; |
114 | 8.37k | for (j = 0; j < v; j++) |
115 | 8.28k | B[j] = Ai[j % u]; |
116 | 73.7k | for (j = 0; j < Ilen; j += v) { |
117 | 73.6k | unsigned char *Ij = I + j; |
118 | 73.6k | uint16_t c = 1; |
119 | | |
120 | | /* Work out Ij = Ij + B + 1 */ |
121 | 7.56M | for (k = v; k > 0;) { |
122 | 7.48M | k--; |
123 | 7.48M | c += Ij[k] + B[k]; |
124 | 7.48M | Ij[k] = (unsigned char)c; |
125 | 7.48M | c >>= 8; |
126 | 7.48M | } |
127 | 73.6k | } |
128 | 91 | } |
129 | | |
130 | 141 | end: |
131 | 141 | OPENSSL_free(Ai); |
132 | 141 | OPENSSL_free(B); |
133 | 141 | OPENSSL_free(D); |
134 | 141 | OPENSSL_free(I); |
135 | 141 | EVP_MD_CTX_free(ctx); |
136 | 141 | return ret; |
137 | 138 | } |
138 | | |
139 | | static void *kdf_pkcs12_new(void *provctx) |
140 | 148 | { |
141 | 148 | KDF_PKCS12 *ctx; |
142 | | |
143 | 148 | if (!ossl_prov_is_running()) |
144 | 0 | return NULL; |
145 | | |
146 | 148 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
147 | 148 | if (ctx == NULL) { |
148 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
149 | 0 | return NULL; |
150 | 0 | } |
151 | 148 | ctx->provctx = provctx; |
152 | 148 | return ctx; |
153 | 148 | } |
154 | | |
155 | | static void kdf_pkcs12_cleanup(KDF_PKCS12 *ctx) |
156 | 148 | { |
157 | 148 | ossl_prov_digest_reset(&ctx->digest); |
158 | 148 | OPENSSL_free(ctx->salt); |
159 | 148 | OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
160 | 148 | memset(ctx, 0, sizeof(*ctx)); |
161 | 148 | } |
162 | | |
163 | | static void kdf_pkcs12_free(void *vctx) |
164 | 148 | { |
165 | 148 | KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx; |
166 | | |
167 | 148 | if (ctx != NULL) { |
168 | 148 | kdf_pkcs12_cleanup(ctx); |
169 | 148 | OPENSSL_free(ctx); |
170 | 148 | } |
171 | 148 | } |
172 | | |
173 | | static void kdf_pkcs12_reset(void *vctx) |
174 | 0 | { |
175 | 0 | KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx; |
176 | 0 | void *provctx = ctx->provctx; |
177 | |
|
178 | 0 | kdf_pkcs12_cleanup(ctx); |
179 | 0 | ctx->provctx = provctx; |
180 | 0 | } |
181 | | |
182 | | static void *kdf_pkcs12_dup(void *vctx) |
183 | 0 | { |
184 | 0 | const KDF_PKCS12 *src = (const KDF_PKCS12 *)vctx; |
185 | 0 | KDF_PKCS12 *dest; |
186 | |
|
187 | 0 | dest = kdf_pkcs12_new(src->provctx); |
188 | 0 | if (dest != NULL) { |
189 | 0 | if (!ossl_prov_memdup(src->salt, src->salt_len, |
190 | 0 | &dest->salt, &dest->salt_len) |
191 | 0 | || !ossl_prov_memdup(src->pass, src->pass_len, |
192 | 0 | &dest->pass , &dest->pass_len) |
193 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
194 | 0 | goto err; |
195 | 0 | dest->iter = src->iter; |
196 | 0 | dest->id = src->id; |
197 | 0 | } |
198 | 0 | return dest; |
199 | | |
200 | 0 | err: |
201 | 0 | kdf_pkcs12_free(dest); |
202 | 0 | return NULL; |
203 | 0 | } |
204 | | |
205 | | static int pkcs12kdf_set_membuf(unsigned char **buffer, size_t *buflen, |
206 | | const OSSL_PARAM *p) |
207 | 282 | { |
208 | 282 | OPENSSL_clear_free(*buffer, *buflen); |
209 | 282 | *buffer = NULL; |
210 | 282 | *buflen = 0; |
211 | | |
212 | 282 | if (p->data_size == 0) { |
213 | 43 | if ((*buffer = OPENSSL_malloc(1)) == NULL) { |
214 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); |
215 | 0 | return 0; |
216 | 0 | } |
217 | 239 | } else if (p->data != NULL) { |
218 | 239 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
219 | 0 | return 0; |
220 | 239 | } |
221 | 282 | return 1; |
222 | 282 | } |
223 | | |
224 | | static int kdf_pkcs12_derive(void *vctx, unsigned char *key, size_t keylen, |
225 | | const OSSL_PARAM params[]) |
226 | 141 | { |
227 | 141 | KDF_PKCS12 *ctx = (KDF_PKCS12 *)vctx; |
228 | 141 | const EVP_MD *md; |
229 | | |
230 | 141 | if (!ossl_prov_is_running() || !kdf_pkcs12_set_ctx_params(ctx, params)) |
231 | 0 | return 0; |
232 | | |
233 | 141 | if (ctx->pass == NULL) { |
234 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
235 | 0 | return 0; |
236 | 0 | } |
237 | | |
238 | 141 | if (ctx->salt == NULL) { |
239 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
240 | 0 | return 0; |
241 | 0 | } |
242 | | |
243 | 141 | md = ossl_prov_digest_md(&ctx->digest); |
244 | 141 | return pkcs12kdf_derive(ctx->pass, ctx->pass_len, ctx->salt, ctx->salt_len, |
245 | 141 | ctx->id, ctx->iter, md, key, keylen); |
246 | 141 | } |
247 | | |
248 | | static int kdf_pkcs12_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
249 | 0 | { |
250 | 0 | const OSSL_PARAM *p; |
251 | 0 | KDF_PKCS12 *ctx = vctx; |
252 | 0 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
253 | |
|
254 | 0 | if (params == NULL) |
255 | 0 | return 1; |
256 | | |
257 | 0 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
258 | 0 | return 0; |
259 | | |
260 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PASSWORD)) != NULL) |
261 | 0 | if (!pkcs12kdf_set_membuf(&ctx->pass, &ctx->pass_len, p)) |
262 | 0 | return 0; |
263 | | |
264 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) |
265 | 0 | if (!pkcs12kdf_set_membuf(&ctx->salt, &ctx->salt_len,p)) |
266 | 0 | return 0; |
267 | | |
268 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PKCS12_ID)) != NULL) |
269 | 0 | if (!OSSL_PARAM_get_int(p, &ctx->id)) |
270 | 0 | return 0; |
271 | | |
272 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_ITER)) != NULL) |
273 | 0 | if (!OSSL_PARAM_get_uint64(p, &ctx->iter)) |
274 | 0 | return 0; |
275 | 0 | return 1; |
276 | 0 | } |
277 | | |
278 | | static const OSSL_PARAM *kdf_pkcs12_settable_ctx_params( |
279 | | ossl_unused void *ctx, ossl_unused void *provctx) |
280 | 148 | { |
281 | 148 | static const OSSL_PARAM known_settable_ctx_params[] = { |
282 | 148 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
283 | 148 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), |
284 | 148 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0), |
285 | 148 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), |
286 | 148 | OSSL_PARAM_uint64(OSSL_KDF_PARAM_ITER, NULL), |
287 | 148 | OSSL_PARAM_int(OSSL_KDF_PARAM_PKCS12_ID, NULL), |
288 | 148 | OSSL_PARAM_END |
289 | 148 | }; |
290 | 148 | return known_settable_ctx_params; |
291 | 148 | } |
292 | | |
293 | | static int kdf_pkcs12_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
294 | 0 | { |
295 | 0 | OSSL_PARAM *p; |
296 | |
|
297 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) |
298 | 0 | return OSSL_PARAM_set_size_t(p, SIZE_MAX); |
299 | 0 | return -2; |
300 | 0 | } |
301 | | |
302 | | static const OSSL_PARAM *kdf_pkcs12_gettable_ctx_params( |
303 | | ossl_unused void *ctx, ossl_unused void *provctx) |
304 | 0 | { |
305 | 0 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
306 | 0 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
307 | 0 | OSSL_PARAM_END |
308 | 0 | }; |
309 | 0 | return known_gettable_ctx_params; |
310 | 0 | } |
311 | | |
312 | | const OSSL_DISPATCH ossl_kdf_pkcs12_functions[] = { |
313 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pkcs12_new }, |
314 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pkcs12_dup }, |
315 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pkcs12_free }, |
316 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pkcs12_reset }, |
317 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pkcs12_derive }, |
318 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
319 | | (void(*)(void))kdf_pkcs12_settable_ctx_params }, |
320 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pkcs12_set_ctx_params }, |
321 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
322 | | (void(*)(void))kdf_pkcs12_gettable_ctx_params }, |
323 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pkcs12_get_ctx_params }, |
324 | | { 0, NULL } |
325 | | }; |