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