/src/openssl/providers/implementations/kdfs/pvkkdf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-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 | | #include <string.h> |
12 | | #include <openssl/evp.h> |
13 | | #include <openssl/core_names.h> |
14 | | #include <openssl/proverr.h> |
15 | | #include <openssl/err.h> |
16 | | #include "internal/common.h" |
17 | | #include "internal/numbers.h" /* SIZE_MAX */ |
18 | | #include "prov/provider_ctx.h" |
19 | | #include "prov/providercommon.h" |
20 | | #include "prov/implementations.h" |
21 | | #include "prov/provider_util.h" |
22 | | |
23 | | static OSSL_FUNC_kdf_newctx_fn kdf_pvk_new; |
24 | | static OSSL_FUNC_kdf_dupctx_fn kdf_pvk_dup; |
25 | | static OSSL_FUNC_kdf_freectx_fn kdf_pvk_free; |
26 | | static OSSL_FUNC_kdf_reset_fn kdf_pvk_reset; |
27 | | static OSSL_FUNC_kdf_derive_fn kdf_pvk_derive; |
28 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_pvk_settable_ctx_params; |
29 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_pvk_set_ctx_params; |
30 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_pvk_gettable_ctx_params; |
31 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_pvk_get_ctx_params; |
32 | | |
33 | | typedef struct { |
34 | | void *provctx; |
35 | | unsigned char *pass; |
36 | | size_t pass_len; |
37 | | unsigned char *salt; |
38 | | size_t salt_len; |
39 | | PROV_DIGEST digest; |
40 | | } KDF_PVK; |
41 | | |
42 | | static void kdf_pvk_init(KDF_PVK *ctx); |
43 | | |
44 | | static void *kdf_pvk_new(void *provctx) |
45 | 0 | { |
46 | 0 | KDF_PVK *ctx; |
47 | |
|
48 | 0 | if (!ossl_prov_is_running()) |
49 | 0 | return NULL; |
50 | | |
51 | 0 | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
52 | 0 | if (ctx == NULL) |
53 | 0 | return NULL; |
54 | 0 | ctx->provctx = provctx; |
55 | 0 | kdf_pvk_init(ctx); |
56 | 0 | return ctx; |
57 | 0 | } |
58 | | |
59 | | static void kdf_pvk_cleanup(KDF_PVK *ctx) |
60 | 0 | { |
61 | 0 | ossl_prov_digest_reset(&ctx->digest); |
62 | 0 | OPENSSL_free(ctx->salt); |
63 | 0 | OPENSSL_clear_free(ctx->pass, ctx->pass_len); |
64 | 0 | OPENSSL_cleanse(ctx, sizeof(*ctx)); |
65 | 0 | } |
66 | | |
67 | | static void kdf_pvk_free(void *vctx) |
68 | 0 | { |
69 | 0 | KDF_PVK *ctx = (KDF_PVK *)vctx; |
70 | |
|
71 | 0 | if (ctx != NULL) { |
72 | 0 | kdf_pvk_cleanup(ctx); |
73 | 0 | OPENSSL_free(ctx); |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | | static void *kdf_pvk_dup(void *vctx) |
78 | 0 | { |
79 | 0 | const KDF_PVK *src = (const KDF_PVK *)vctx; |
80 | 0 | KDF_PVK *dest; |
81 | |
|
82 | 0 | dest = kdf_pvk_new(src->provctx); |
83 | 0 | if (dest != NULL) |
84 | 0 | if (!ossl_prov_memdup(src->salt, src->salt_len, |
85 | 0 | &dest->salt, &dest->salt_len) |
86 | 0 | || !ossl_prov_memdup(src->pass, src->pass_len, |
87 | 0 | &dest->pass , &dest->pass_len) |
88 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
89 | 0 | goto err; |
90 | 0 | return dest; |
91 | | |
92 | 0 | err: |
93 | 0 | kdf_pvk_free(dest); |
94 | 0 | return NULL; |
95 | 0 | } |
96 | | |
97 | | static void kdf_pvk_reset(void *vctx) |
98 | 0 | { |
99 | 0 | KDF_PVK *ctx = (KDF_PVK *)vctx; |
100 | 0 | void *provctx = ctx->provctx; |
101 | |
|
102 | 0 | kdf_pvk_cleanup(ctx); |
103 | 0 | ctx->provctx = provctx; |
104 | 0 | kdf_pvk_init(ctx); |
105 | 0 | } |
106 | | |
107 | | static void kdf_pvk_init(KDF_PVK *ctx) |
108 | 0 | { |
109 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
110 | 0 | OSSL_LIB_CTX *provctx = PROV_LIBCTX_OF(ctx->provctx); |
111 | |
|
112 | 0 | params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, |
113 | 0 | SN_sha1, 0); |
114 | 0 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
115 | | /* This is an error, but there is no way to indicate such directly */ |
116 | 0 | ossl_prov_digest_reset(&ctx->digest); |
117 | 0 | } |
118 | | |
119 | | static int pvk_set_membuf(unsigned char **buffer, size_t *buflen, |
120 | | const OSSL_PARAM *p) |
121 | 0 | { |
122 | 0 | OPENSSL_clear_free(*buffer, *buflen); |
123 | 0 | *buffer = NULL; |
124 | 0 | *buflen = 0; |
125 | |
|
126 | 0 | if (p->data_size == 0) { |
127 | 0 | if ((*buffer = OPENSSL_malloc(1)) == NULL) |
128 | 0 | return 0; |
129 | 0 | } else if (p->data != NULL) { |
130 | 0 | if (!OSSL_PARAM_get_octet_string(p, (void **)buffer, 0, buflen)) |
131 | 0 | return 0; |
132 | 0 | } |
133 | 0 | return 1; |
134 | 0 | } |
135 | | |
136 | | static int kdf_pvk_derive(void *vctx, unsigned char *key, size_t keylen, |
137 | | const OSSL_PARAM params[]) |
138 | 0 | { |
139 | 0 | KDF_PVK *ctx = (KDF_PVK *)vctx; |
140 | 0 | const EVP_MD *md; |
141 | 0 | EVP_MD_CTX *mctx; |
142 | 0 | int res; |
143 | |
|
144 | 0 | if (!ossl_prov_is_running() || !kdf_pvk_set_ctx_params(ctx, params)) |
145 | 0 | return 0; |
146 | | |
147 | 0 | if (ctx->pass == NULL) { |
148 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_PASS); |
149 | 0 | return 0; |
150 | 0 | } |
151 | | |
152 | 0 | if (ctx->salt == NULL) { |
153 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SALT); |
154 | 0 | return 0; |
155 | 0 | } |
156 | | |
157 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
158 | 0 | if (md == NULL) { |
159 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST); |
160 | 0 | return 0; |
161 | 0 | } |
162 | 0 | res = EVP_MD_get_size(md); |
163 | 0 | if (res <= 0) { |
164 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_BAD_LENGTH); |
165 | 0 | return 0; |
166 | 0 | } |
167 | 0 | if ((size_t)res > keylen) { |
168 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_LENGTH_TOO_LARGE); |
169 | 0 | return 0; |
170 | 0 | } |
171 | | |
172 | 0 | mctx = EVP_MD_CTX_new(); |
173 | 0 | res = mctx != NULL |
174 | 0 | && EVP_DigestInit_ex(mctx, md, NULL) |
175 | 0 | && EVP_DigestUpdate(mctx, ctx->salt, ctx->salt_len) |
176 | 0 | && EVP_DigestUpdate(mctx, ctx->pass, ctx->pass_len) |
177 | 0 | && EVP_DigestFinal_ex(mctx, key, NULL); |
178 | 0 | EVP_MD_CTX_free(mctx); |
179 | 0 | return res; |
180 | 0 | } |
181 | | |
182 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
183 | | #ifndef pvk_set_ctx_params_list |
184 | | static const OSSL_PARAM pvk_set_ctx_params_list[] = { |
185 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), |
186 | | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), |
187 | | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD, NULL, 0), |
188 | | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), |
189 | | OSSL_PARAM_END |
190 | | }; |
191 | | #endif |
192 | | |
193 | | #ifndef pvk_set_ctx_params_st |
194 | | struct pvk_set_ctx_params_st { |
195 | | OSSL_PARAM *digest; |
196 | | OSSL_PARAM *engine; |
197 | | OSSL_PARAM *pass; |
198 | | OSSL_PARAM *propq; |
199 | | OSSL_PARAM *salt; |
200 | | }; |
201 | | #endif |
202 | | |
203 | | #ifndef pvk_set_ctx_params_decoder |
204 | | static int pvk_set_ctx_params_decoder |
205 | | (const OSSL_PARAM *p, struct pvk_set_ctx_params_st *r) |
206 | 0 | { |
207 | 0 | const char *s; |
208 | |
|
209 | 0 | memset(r, 0, sizeof(*r)); |
210 | 0 | if (p != NULL) |
211 | 0 | for (; (s = p->key) != NULL; p++) |
212 | 0 | switch(s[0]) { |
213 | 0 | default: |
214 | 0 | break; |
215 | 0 | case 'd': |
216 | 0 | if (ossl_likely(strcmp("igest", s + 1) == 0)) { |
217 | | /* KDF_PARAM_DIGEST */ |
218 | 0 | if (ossl_unlikely(r->digest != NULL)) { |
219 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
220 | 0 | "param %s is repeated", s); |
221 | 0 | return 0; |
222 | 0 | } |
223 | 0 | r->digest = (OSSL_PARAM *)p; |
224 | 0 | } |
225 | 0 | break; |
226 | 0 | case 'e': |
227 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
228 | | /* ALG_PARAM_ENGINE */ |
229 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
230 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
231 | 0 | "param %s is repeated", s); |
232 | 0 | return 0; |
233 | 0 | } |
234 | 0 | r->engine = (OSSL_PARAM *)p; |
235 | 0 | } |
236 | 0 | break; |
237 | 0 | case 'p': |
238 | 0 | switch(s[1]) { |
239 | 0 | default: |
240 | 0 | break; |
241 | 0 | case 'a': |
242 | 0 | if (ossl_likely(strcmp("ss", s + 2) == 0)) { |
243 | | /* KDF_PARAM_PASSWORD */ |
244 | 0 | if (ossl_unlikely(r->pass != NULL)) { |
245 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
246 | 0 | "param %s is repeated", s); |
247 | 0 | return 0; |
248 | 0 | } |
249 | 0 | r->pass = (OSSL_PARAM *)p; |
250 | 0 | } |
251 | 0 | break; |
252 | 0 | case 'r': |
253 | 0 | if (ossl_likely(strcmp("operties", s + 2) == 0)) { |
254 | | /* KDF_PARAM_PROPERTIES */ |
255 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
256 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
257 | 0 | "param %s is repeated", s); |
258 | 0 | return 0; |
259 | 0 | } |
260 | 0 | r->propq = (OSSL_PARAM *)p; |
261 | 0 | } |
262 | 0 | } |
263 | 0 | break; |
264 | 0 | case 's': |
265 | 0 | if (ossl_likely(strcmp("alt", s + 1) == 0)) { |
266 | | /* KDF_PARAM_SALT */ |
267 | 0 | if (ossl_unlikely(r->salt != NULL)) { |
268 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
269 | 0 | "param %s is repeated", s); |
270 | 0 | return 0; |
271 | 0 | } |
272 | 0 | r->salt = (OSSL_PARAM *)p; |
273 | 0 | } |
274 | 0 | } |
275 | 0 | return 1; |
276 | 0 | } |
277 | | #endif |
278 | | /* End of machine generated */ |
279 | | |
280 | | static int kdf_pvk_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
281 | 0 | { |
282 | 0 | struct pvk_set_ctx_params_st p; |
283 | 0 | KDF_PVK *ctx = vctx; |
284 | 0 | OSSL_LIB_CTX *provctx; |
285 | |
|
286 | 0 | if (ctx == NULL || !pvk_set_ctx_params_decoder(params, &p)) |
287 | 0 | return 0; |
288 | | |
289 | 0 | provctx = PROV_LIBCTX_OF(ctx->provctx); |
290 | |
|
291 | 0 | if (!ossl_prov_digest_load(&ctx->digest, p.digest, p.propq, p.engine, |
292 | 0 | provctx)) |
293 | 0 | return 0; |
294 | | |
295 | 0 | if (p.pass != NULL && !pvk_set_membuf(&ctx->pass, &ctx->pass_len, p.pass)) |
296 | 0 | return 0; |
297 | | |
298 | 0 | if (p.salt != NULL && !pvk_set_membuf(&ctx->salt, &ctx->salt_len, p.salt)) |
299 | 0 | return 0; |
300 | | |
301 | 0 | return 1; |
302 | 0 | } |
303 | | |
304 | | static const OSSL_PARAM *kdf_pvk_settable_ctx_params(ossl_unused void *ctx, |
305 | | ossl_unused void *p_ctx) |
306 | 0 | { |
307 | 0 | return pvk_set_ctx_params_list; |
308 | 0 | } |
309 | | |
310 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
311 | | #ifndef pvk_get_ctx_params_list |
312 | | static const OSSL_PARAM pvk_get_ctx_params_list[] = { |
313 | | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
314 | | OSSL_PARAM_END |
315 | | }; |
316 | | #endif |
317 | | |
318 | | #ifndef pvk_get_ctx_params_st |
319 | | struct pvk_get_ctx_params_st { |
320 | | OSSL_PARAM *size; |
321 | | }; |
322 | | #endif |
323 | | |
324 | | #ifndef pvk_get_ctx_params_decoder |
325 | | static int pvk_get_ctx_params_decoder |
326 | | (const OSSL_PARAM *p, struct pvk_get_ctx_params_st *r) |
327 | 0 | { |
328 | 0 | const char *s; |
329 | |
|
330 | 0 | memset(r, 0, sizeof(*r)); |
331 | 0 | if (p != NULL) |
332 | 0 | for (; (s = p->key) != NULL; p++) |
333 | 0 | if (ossl_likely(strcmp("size", s + 0) == 0)) { |
334 | | /* KDF_PARAM_SIZE */ |
335 | 0 | if (ossl_unlikely(r->size != NULL)) { |
336 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
337 | 0 | "param %s is repeated", s); |
338 | 0 | return 0; |
339 | 0 | } |
340 | 0 | r->size = (OSSL_PARAM *)p; |
341 | 0 | } |
342 | 0 | return 1; |
343 | 0 | } |
344 | | #endif |
345 | | /* End of machine generated */ |
346 | | |
347 | | static int kdf_pvk_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
348 | 0 | { |
349 | 0 | struct pvk_get_ctx_params_st p; |
350 | 0 | KDF_PVK *ctx = vctx; |
351 | |
|
352 | 0 | if (ctx == NULL || !pvk_get_ctx_params_decoder(params, &p)) |
353 | 0 | return 0; |
354 | | |
355 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, SIZE_MAX)) |
356 | 0 | return 0; |
357 | 0 | return 1; |
358 | 0 | } |
359 | | |
360 | | static const OSSL_PARAM *kdf_pvk_gettable_ctx_params(ossl_unused void *ctx, |
361 | | ossl_unused void *p_ctx) |
362 | 0 | { |
363 | 0 | return pvk_get_ctx_params_list; |
364 | 0 | } |
365 | | |
366 | | const OSSL_DISPATCH ossl_kdf_pvk_functions[] = { |
367 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_pvk_new }, |
368 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_pvk_dup }, |
369 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_pvk_free }, |
370 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_pvk_reset }, |
371 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_pvk_derive }, |
372 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
373 | | (void(*)(void))kdf_pvk_settable_ctx_params }, |
374 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_pvk_set_ctx_params }, |
375 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
376 | | (void(*)(void))kdf_pvk_gettable_ctx_params }, |
377 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_pvk_get_ctx_params }, |
378 | | OSSL_DISPATCH_END |
379 | | }; |