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