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