/src/openssl/crypto/pkcs12/p12_mutl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2026 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 | | * HMAC low level APIs are deprecated for public use, but still ok for internal |
12 | | * use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <stdio.h> |
17 | | #include "internal/cryptlib.h" |
18 | | #include "crypto/evp.h" |
19 | | #include <openssl/crypto.h> |
20 | | #include <openssl/hmac.h> |
21 | | #include <openssl/rand.h> |
22 | | #include <openssl/pkcs12.h> |
23 | | #include "p12_local.h" |
24 | | |
25 | | #include <crypto/asn1.h> |
26 | | |
27 | | static int pkcs12_pbmac1_pbkdf2_key_gen(const char *pass, int passlen, |
28 | | unsigned char *salt, int saltlen, |
29 | | int id, int iter, int keylen, |
30 | | unsigned char *out, |
31 | | const EVP_MD *md_type, |
32 | | OSSL_LIB_CTX *libctx, const char *propq); |
33 | | |
34 | | int PKCS12_mac_present(const PKCS12 *p12) |
35 | 0 | { |
36 | 0 | return p12->mac ? 1 : 0; |
37 | 0 | } |
38 | | |
39 | | void PKCS12_get0_mac(const ASN1_OCTET_STRING **pmac, |
40 | | const X509_ALGOR **pmacalg, |
41 | | const ASN1_OCTET_STRING **psalt, |
42 | | const ASN1_INTEGER **piter, |
43 | | const PKCS12 *p12) |
44 | 0 | { |
45 | 0 | if (p12->mac) { |
46 | 0 | X509_SIG_get0(p12->mac->dinfo, pmacalg, pmac); |
47 | 0 | if (psalt) |
48 | 0 | *psalt = p12->mac->salt; |
49 | 0 | if (piter) |
50 | 0 | *piter = p12->mac->iter; |
51 | 0 | } else { |
52 | 0 | if (pmac) |
53 | 0 | *pmac = NULL; |
54 | 0 | if (pmacalg) |
55 | 0 | *pmacalg = NULL; |
56 | 0 | if (psalt) |
57 | 0 | *psalt = NULL; |
58 | 0 | if (piter) |
59 | 0 | *piter = NULL; |
60 | 0 | } |
61 | 0 | } |
62 | | |
63 | 0 | #define TK26_MAC_KEY_LEN 32 |
64 | | |
65 | | static int pkcs12_gen_gost_mac_key(const char *pass, int passlen, |
66 | | const unsigned char *salt, int saltlen, |
67 | | int iter, int keylen, unsigned char *key, |
68 | | const EVP_MD *digest) |
69 | 0 | { |
70 | 0 | unsigned char out[96]; |
71 | |
|
72 | 0 | if (keylen != TK26_MAC_KEY_LEN) { |
73 | 0 | return 0; |
74 | 0 | } |
75 | | |
76 | 0 | if (!PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, |
77 | 0 | digest, sizeof(out), out)) { |
78 | 0 | return 0; |
79 | 0 | } |
80 | 0 | memcpy(key, out + sizeof(out) - TK26_MAC_KEY_LEN, TK26_MAC_KEY_LEN); |
81 | 0 | OPENSSL_cleanse(out, sizeof(out)); |
82 | 0 | return 1; |
83 | 0 | } |
84 | | |
85 | | PBKDF2PARAM *PBMAC1_get1_pbkdf2_param(const X509_ALGOR *macalg) |
86 | 0 | { |
87 | 0 | PBMAC1PARAM *param = NULL; |
88 | 0 | PBKDF2PARAM *pbkdf2_param = NULL; |
89 | 0 | const ASN1_OBJECT *kdf_oid; |
90 | |
|
91 | 0 | param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter); |
92 | 0 | if (param == NULL) { |
93 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT); |
94 | 0 | return NULL; |
95 | 0 | } |
96 | | |
97 | 0 | X509_ALGOR_get0(&kdf_oid, NULL, NULL, param->keyDerivationFunc); |
98 | 0 | if (OBJ_obj2nid(kdf_oid) != NID_id_pbkdf2) { |
99 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT); |
100 | 0 | PBMAC1PARAM_free(param); |
101 | 0 | return NULL; |
102 | 0 | } |
103 | | |
104 | 0 | pbkdf2_param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM), |
105 | 0 | param->keyDerivationFunc->parameter); |
106 | 0 | PBMAC1PARAM_free(param); |
107 | |
|
108 | 0 | return pbkdf2_param; |
109 | 0 | } |
110 | | |
111 | | static int PBMAC1_PBKDF2_HMAC(OSSL_LIB_CTX *ctx, const char *propq, |
112 | | const char *pass, int passlen, |
113 | | const X509_ALGOR *macalg, unsigned char *key) |
114 | 0 | { |
115 | 0 | PBKDF2PARAM *pbkdf2_param = NULL; |
116 | 0 | const ASN1_OBJECT *kdf_hmac_oid; |
117 | 0 | int kdf_hmac_nid; |
118 | 0 | int ret = -1; |
119 | 0 | int keylen = 0; |
120 | 0 | EVP_MD *kdf_md = NULL; |
121 | 0 | const ASN1_OCTET_STRING *pbkdf2_salt = NULL; |
122 | |
|
123 | 0 | pbkdf2_param = PBMAC1_get1_pbkdf2_param(macalg); |
124 | 0 | if (pbkdf2_param == NULL) { |
125 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED); |
126 | 0 | goto err; |
127 | 0 | } |
128 | | |
129 | 0 | if (pbkdf2_param->prf == NULL) { |
130 | 0 | kdf_hmac_nid = NID_hmacWithSHA1; |
131 | 0 | } else { |
132 | 0 | X509_ALGOR_get0(&kdf_hmac_oid, NULL, NULL, pbkdf2_param->prf); |
133 | 0 | kdf_hmac_nid = OBJ_obj2nid(kdf_hmac_oid); |
134 | 0 | } |
135 | |
|
136 | 0 | kdf_md = EVP_MD_fetch(ctx, OBJ_nid2sn(ossl_hmac2mdnid(kdf_hmac_nid)), propq); |
137 | 0 | if (kdf_md == NULL) { |
138 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_FETCH_FAILED); |
139 | 0 | goto err; |
140 | 0 | } |
141 | | |
142 | | /* Validate salt is an OCTET STRING choice */ |
143 | 0 | if (pbkdf2_param->salt == NULL |
144 | 0 | || pbkdf2_param->salt->type != V_ASN1_OCTET_STRING) { |
145 | 0 | ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR, "Invalid Salt"); |
146 | 0 | goto err; |
147 | 0 | } |
148 | 0 | pbkdf2_salt = pbkdf2_param->salt->value.octet_string; |
149 | | |
150 | | /* RFC 9879 specifies missing key length as invalid */ |
151 | 0 | if (pbkdf2_param->keylength != NULL) |
152 | 0 | keylen = ASN1_INTEGER_get(pbkdf2_param->keylength); |
153 | | /* RFC 9879 specifies too short key length as untrustworthy too */ |
154 | 0 | if (keylen < 20 || keylen > EVP_MAX_MD_SIZE) { |
155 | 0 | ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR, |
156 | 0 | "Invalid Key length (%d is not in the range 20..64)", keylen); |
157 | 0 | goto err; |
158 | 0 | } |
159 | | |
160 | 0 | if (PKCS5_PBKDF2_HMAC(pass, passlen, pbkdf2_salt->data, pbkdf2_salt->length, |
161 | 0 | ASN1_INTEGER_get(pbkdf2_param->iter), kdf_md, keylen, key) |
162 | 0 | <= 0) { |
163 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR); |
164 | 0 | goto err; |
165 | 0 | } |
166 | 0 | ret = keylen; |
167 | |
|
168 | 0 | err: |
169 | 0 | EVP_MD_free(kdf_md); |
170 | 0 | PBKDF2PARAM_free(pbkdf2_param); |
171 | |
|
172 | 0 | return ret; |
173 | 0 | } |
174 | | |
175 | | /* Generate a MAC, also used for verification */ |
176 | | static int pkcs12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
177 | | unsigned char *mac, unsigned int *maclen, |
178 | | int pbmac1_md_nid, int pbmac1_kdf_nid, |
179 | | int (*pkcs12_key_gen)(const char *pass, int passlen, |
180 | | unsigned char *salt, int slen, |
181 | | int id, int iter, int n, |
182 | | unsigned char *out, |
183 | | const EVP_MD *md_type, |
184 | | OSSL_LIB_CTX *libctx, |
185 | | const char *propq), |
186 | | OSSL_LIB_CTX *libctx, const char *propq) |
187 | 0 | { |
188 | 0 | int ret = 0; |
189 | 0 | EVP_MD *md; |
190 | 0 | unsigned char key[EVP_MAX_MD_SIZE], *salt; |
191 | 0 | int saltlen, iter; |
192 | 0 | char md_name[80]; |
193 | 0 | int keylen = 0; |
194 | 0 | int md_nid = NID_undef; |
195 | 0 | const X509_ALGOR *macalg; |
196 | 0 | const ASN1_OBJECT *macoid; |
197 | 0 | size_t md_sz, outlen; |
198 | |
|
199 | 0 | if (!PKCS7_type_is_data(p12->authsafes)) { |
200 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); |
201 | 0 | return 0; |
202 | 0 | } |
203 | | |
204 | 0 | if (p12->authsafes->d.data == NULL) { |
205 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR); |
206 | 0 | return 0; |
207 | 0 | } |
208 | | |
209 | 0 | salt = p12->mac->salt->data; |
210 | 0 | saltlen = p12->mac->salt->length; |
211 | 0 | if (p12->mac->iter == NULL) |
212 | 0 | iter = 1; |
213 | 0 | else |
214 | 0 | iter = ASN1_INTEGER_get(p12->mac->iter); |
215 | 0 | X509_SIG_get0(p12->mac->dinfo, &macalg, NULL); |
216 | 0 | X509_ALGOR_get0(&macoid, NULL, NULL, macalg); |
217 | 0 | if (OBJ_obj2nid(macoid) == NID_pbmac1) { |
218 | 0 | if (OBJ_obj2txt(md_name, sizeof(md_name), OBJ_nid2obj(pbmac1_md_nid), 0) < 0) |
219 | 0 | return 0; |
220 | 0 | } else { |
221 | 0 | if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0) |
222 | 0 | return 0; |
223 | 0 | } |
224 | 0 | md = EVP_MD_fetch(libctx, md_name, propq); |
225 | |
|
226 | 0 | if (md == NULL) { |
227 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM); |
228 | 0 | return 0; |
229 | 0 | } |
230 | | |
231 | 0 | keylen = EVP_MD_get_size(md); |
232 | 0 | md_nid = EVP_MD_get_type(md); |
233 | 0 | if (keylen <= 0) |
234 | 0 | goto err; |
235 | 0 | md_sz = keylen; |
236 | | |
237 | | /* For PBMAC1 we use a special keygen callback if not provided (e.g. on verification) */ |
238 | 0 | if (pbmac1_md_nid != NID_undef && pkcs12_key_gen == NULL) { |
239 | 0 | keylen = PBMAC1_PBKDF2_HMAC(libctx, propq, pass, passlen, macalg, key); |
240 | 0 | if (keylen < 0) |
241 | 0 | goto err; |
242 | 0 | } else if ((md_nid == NID_id_GostR3411_94 |
243 | 0 | || md_nid == NID_id_GostR3411_2012_256 |
244 | 0 | || md_nid == NID_id_GostR3411_2012_512) |
245 | 0 | && ossl_safe_getenv("LEGACY_GOST_PKCS12") == NULL) { |
246 | 0 | keylen = TK26_MAC_KEY_LEN; |
247 | 0 | if (!pkcs12_gen_gost_mac_key(pass, passlen, salt, saltlen, iter, |
248 | 0 | keylen, key, md)) { |
249 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
250 | 0 | goto err; |
251 | 0 | } |
252 | 0 | } else { |
253 | 0 | EVP_MD *hmac_md = md; |
254 | 0 | int fetched = 0; |
255 | |
|
256 | 0 | if (pbmac1_kdf_nid != NID_undef) { |
257 | 0 | char hmac_md_name[128]; |
258 | |
|
259 | 0 | if (OBJ_obj2txt(hmac_md_name, sizeof(hmac_md_name), OBJ_nid2obj(pbmac1_kdf_nid), 0) < 0) |
260 | 0 | goto err; |
261 | 0 | hmac_md = EVP_MD_fetch(libctx, hmac_md_name, propq); |
262 | 0 | if (hmac_md == NULL) |
263 | 0 | goto err; |
264 | 0 | fetched = 1; |
265 | 0 | } |
266 | 0 | if (pkcs12_key_gen != NULL) { |
267 | 0 | int res = (*pkcs12_key_gen)(pass, passlen, salt, saltlen, PKCS12_MAC_ID, |
268 | 0 | iter, keylen, key, hmac_md, libctx, propq); |
269 | |
|
270 | 0 | if (fetched) |
271 | 0 | EVP_MD_free(hmac_md); |
272 | 0 | if (res != 1) { |
273 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
274 | 0 | goto err; |
275 | 0 | } |
276 | 0 | } else { |
277 | 0 | if (fetched) |
278 | 0 | EVP_MD_free(hmac_md); |
279 | | /* Default to UTF-8 password */ |
280 | 0 | if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID, |
281 | 0 | iter, keylen, key, md, libctx, propq)) { |
282 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR); |
283 | 0 | goto err; |
284 | 0 | } |
285 | 0 | } |
286 | 0 | } |
287 | 0 | if (EVP_Q_mac(libctx, "HMAC", propq, md_name, NULL, key, keylen, |
288 | 0 | p12->authsafes->d.data->data, p12->authsafes->d.data->length, |
289 | 0 | mac, md_sz, &outlen) |
290 | 0 | == NULL) |
291 | 0 | goto err; |
292 | 0 | if (outlen > UINT_MAX) |
293 | 0 | goto err; |
294 | 0 | *maclen = (unsigned int)outlen; |
295 | 0 | ret = 1; |
296 | 0 | err: |
297 | 0 | OPENSSL_cleanse(key, sizeof(key)); |
298 | 0 | EVP_MD_free(md); |
299 | 0 | return ret; |
300 | 0 | } |
301 | | |
302 | | int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen, |
303 | | unsigned char *mac, unsigned int *maclen) |
304 | 0 | { |
305 | 0 | return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NID_undef, NID_undef, NULL, p12->authsafes->ctx.libctx, p12->authsafes->ctx.propq); |
306 | 0 | } |
307 | | |
308 | | int ossl_pkcs12_verify_mac(PKCS12 *p12, const char *pass, int passlen, |
309 | | OSSL_LIB_CTX *libctx, const char *propq) |
310 | 0 | { |
311 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
312 | 0 | unsigned int maclen; |
313 | 0 | const ASN1_OCTET_STRING *macoct; |
314 | 0 | const X509_ALGOR *macalg; |
315 | 0 | const ASN1_OBJECT *macoid; |
316 | |
|
317 | 0 | if (p12->mac == NULL) { |
318 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT); |
319 | 0 | return 0; |
320 | 0 | } |
321 | | |
322 | 0 | X509_SIG_get0(p12->mac->dinfo, &macalg, NULL); |
323 | 0 | X509_ALGOR_get0(&macoid, NULL, NULL, macalg); |
324 | 0 | if (OBJ_obj2nid(macoid) == NID_pbmac1) { |
325 | 0 | PBMAC1PARAM *param = NULL; |
326 | 0 | const ASN1_OBJECT *hmac_oid; |
327 | 0 | int md_nid = NID_undef; |
328 | |
|
329 | 0 | param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter); |
330 | 0 | if (param == NULL) { |
331 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED); |
332 | 0 | return 0; |
333 | 0 | } |
334 | 0 | X509_ALGOR_get0(&hmac_oid, NULL, NULL, param->messageAuthScheme); |
335 | 0 | md_nid = ossl_hmac2mdnid(OBJ_obj2nid(hmac_oid)); |
336 | |
|
337 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, md_nid, NID_undef, NULL, libctx, propq)) { |
338 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
339 | 0 | PBMAC1PARAM_free(param); |
340 | 0 | return 0; |
341 | 0 | } |
342 | 0 | PBMAC1PARAM_free(param); |
343 | 0 | } else { |
344 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NID_undef, NID_undef, NULL, libctx, propq)) { |
345 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
346 | 0 | return 0; |
347 | 0 | } |
348 | 0 | } |
349 | 0 | X509_SIG_get0(p12->mac->dinfo, NULL, &macoct); |
350 | 0 | if ((maclen != ASN1_STRING_get_length(macoct)) |
351 | 0 | || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0) |
352 | 0 | return 0; |
353 | | |
354 | 0 | return 1; |
355 | 0 | } |
356 | | |
357 | | /* Verify the mac */ |
358 | | int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen) |
359 | 0 | { |
360 | 0 | return ossl_pkcs12_verify_mac(p12, pass, passlen, p12->authsafes->ctx.libctx, p12->authsafes->ctx.propq); |
361 | 0 | } |
362 | | |
363 | | /* Set a mac */ |
364 | | int PKCS12_set_mac(PKCS12 *p12, const char *pass, int passlen, |
365 | | unsigned char *salt, int saltlen, int iter, |
366 | | const EVP_MD *md_type) |
367 | 0 | { |
368 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
369 | 0 | unsigned int maclen; |
370 | 0 | ASN1_OCTET_STRING *macoct; |
371 | |
|
372 | 0 | if (md_type == NULL) |
373 | | /* No need to do a fetch as the md_type is used only to get a NID */ |
374 | 0 | md_type = EVP_sha256(); |
375 | 0 | if (!iter) |
376 | 0 | iter = PKCS12_DEFAULT_ITER; |
377 | 0 | if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) { |
378 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
379 | 0 | return 0; |
380 | 0 | } |
381 | | /* |
382 | | * Note that output mac is forced to UTF-8... |
383 | | */ |
384 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NID_undef, NID_undef, NULL, p12->authsafes->ctx.libctx, p12->authsafes->ctx.propq)) { |
385 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
386 | 0 | return 0; |
387 | 0 | } |
388 | 0 | X509_SIG_getm(p12->mac->dinfo, NULL, &macoct); |
389 | 0 | if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) { |
390 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR); |
391 | 0 | return 0; |
392 | 0 | } |
393 | 0 | return 1; |
394 | 0 | } |
395 | | |
396 | | static int pkcs12_pbmac1_pbkdf2_key_gen(const char *pass, int passlen, |
397 | | unsigned char *salt, int saltlen, |
398 | | int id, int iter, int keylen, |
399 | | unsigned char *out, |
400 | | const EVP_MD *md_type, |
401 | | OSSL_LIB_CTX *libctx, const char *propq) |
402 | 0 | { |
403 | 0 | return ossl_pkcs5_pbkdf2_hmac_ex(pass, passlen, salt, saltlen, iter, md_type, |
404 | 0 | keylen, out, libctx, propq); |
405 | 0 | } |
406 | | |
407 | | static int pkcs12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen, |
408 | | int nid) |
409 | 0 | { |
410 | 0 | X509_ALGOR *macalg; |
411 | |
|
412 | 0 | PKCS12_MAC_DATA_free(p12->mac); |
413 | 0 | p12->mac = NULL; |
414 | |
|
415 | 0 | if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL) |
416 | 0 | return PKCS12_ERROR; |
417 | 0 | if (iter > 1) { |
418 | 0 | if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) { |
419 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
420 | 0 | return 0; |
421 | 0 | } |
422 | 0 | if (!ASN1_INTEGER_set(p12->mac->iter, iter)) { |
423 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
424 | 0 | return 0; |
425 | 0 | } |
426 | 0 | } |
427 | 0 | if (saltlen == 0) |
428 | 0 | saltlen = PKCS12_SALT_LEN; |
429 | 0 | else if (saltlen < 0) |
430 | 0 | return 0; |
431 | 0 | if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL) |
432 | 0 | return 0; |
433 | 0 | p12->mac->salt->length = saltlen; |
434 | 0 | if (salt == NULL) { |
435 | 0 | if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data, |
436 | 0 | (size_t)saltlen, 0) |
437 | 0 | <= 0) |
438 | 0 | return 0; |
439 | 0 | } else { |
440 | 0 | memcpy(p12->mac->salt->data, salt, saltlen); |
441 | 0 | } |
442 | 0 | X509_SIG_getm(p12->mac->dinfo, &macalg, NULL); |
443 | 0 | if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(nid), V_ASN1_NULL, NULL)) { |
444 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
445 | 0 | return 0; |
446 | 0 | } |
447 | | |
448 | 0 | return 1; |
449 | 0 | } |
450 | | |
451 | | /* Set up a mac structure */ |
452 | | int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen, |
453 | | const EVP_MD *md_type) |
454 | 0 | { |
455 | 0 | return pkcs12_setup_mac(p12, iter, salt, saltlen, EVP_MD_get_type(md_type)); |
456 | 0 | } |
457 | | |
458 | | int PKCS12_set_pbmac1_pbkdf2(PKCS12 *p12, const char *pass, int passlen, |
459 | | unsigned char *salt, int saltlen, int iter, |
460 | | const EVP_MD *md_type, const char *prf_md_name) |
461 | 0 | { |
462 | 0 | unsigned char mac[EVP_MAX_MD_SIZE]; |
463 | 0 | unsigned int maclen; |
464 | 0 | ASN1_OCTET_STRING *macoct; |
465 | 0 | X509_ALGOR *alg = NULL; |
466 | 0 | int ret = 0; |
467 | 0 | int prf_md_nid = NID_undef, prf_nid = NID_undef, hmac_nid; |
468 | 0 | unsigned char *known_salt = NULL; |
469 | 0 | int keylen = 0; |
470 | 0 | PBMAC1PARAM *param = NULL; |
471 | 0 | X509_ALGOR *hmac_alg = NULL, *macalg = NULL; |
472 | 0 | OSSL_LIB_CTX *libctx = p12->authsafes->ctx.libctx; |
473 | |
|
474 | 0 | if (md_type == NULL) |
475 | | /* No need to do a fetch as the md_type is used only to get a NID */ |
476 | 0 | md_type = EVP_sha256(); |
477 | |
|
478 | 0 | if (prf_md_name == NULL) |
479 | 0 | prf_md_nid = EVP_MD_get_type(md_type); |
480 | 0 | else |
481 | 0 | prf_md_nid = OBJ_txt2nid(prf_md_name); |
482 | |
|
483 | 0 | if (iter == 0) |
484 | 0 | iter = PKCS12_DEFAULT_ITER; |
485 | |
|
486 | 0 | keylen = EVP_MD_get_size(md_type); |
487 | |
|
488 | 0 | prf_nid = ossl_md2hmacnid(prf_md_nid); |
489 | 0 | hmac_nid = ossl_md2hmacnid(EVP_MD_get_type(md_type)); |
490 | |
|
491 | 0 | if (prf_nid == NID_undef || hmac_nid == NID_undef) { |
492 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM); |
493 | 0 | goto err; |
494 | 0 | } |
495 | | |
496 | 0 | if (salt == NULL) { |
497 | 0 | if (saltlen < 0) { |
498 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_SALT_LENGTH); |
499 | 0 | goto err; |
500 | 0 | } |
501 | 0 | if (saltlen > 0) { |
502 | 0 | known_salt = OPENSSL_malloc(saltlen); |
503 | 0 | if (known_salt == NULL) |
504 | 0 | goto err; |
505 | | |
506 | 0 | if (RAND_bytes_ex(libctx, known_salt, saltlen, 0) <= 0) { |
507 | 0 | ERR_raise(ERR_LIB_PKCS12, ERR_R_RAND_LIB); |
508 | 0 | goto err; |
509 | 0 | } |
510 | 0 | } |
511 | 0 | } |
512 | | |
513 | 0 | param = PBMAC1PARAM_new(); |
514 | 0 | hmac_alg = X509_ALGOR_new(); |
515 | 0 | alg = PKCS5_pbkdf2_set_ex(iter, salt ? salt : known_salt, saltlen, |
516 | 0 | prf_nid, keylen, libctx); |
517 | 0 | if (param == NULL || hmac_alg == NULL || alg == NULL) |
518 | 0 | goto err; |
519 | | |
520 | 0 | if (pkcs12_setup_mac(p12, iter, salt ? salt : known_salt, saltlen, |
521 | 0 | NID_pbmac1) |
522 | 0 | == PKCS12_ERROR) { |
523 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
524 | 0 | goto err; |
525 | 0 | } |
526 | | |
527 | 0 | if (!X509_ALGOR_set0(hmac_alg, OBJ_nid2obj(hmac_nid), V_ASN1_NULL, NULL)) { |
528 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR); |
529 | 0 | goto err; |
530 | 0 | } |
531 | | |
532 | 0 | X509_ALGOR_free(param->keyDerivationFunc); |
533 | 0 | X509_ALGOR_free(param->messageAuthScheme); |
534 | 0 | param->keyDerivationFunc = alg; |
535 | 0 | param->messageAuthScheme = hmac_alg; |
536 | 0 | alg = NULL; |
537 | 0 | hmac_alg = NULL; |
538 | |
|
539 | 0 | X509_SIG_getm(p12->mac->dinfo, &macalg, &macoct); |
540 | 0 | if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), param, &macalg->parameter)) |
541 | 0 | goto err; |
542 | | |
543 | | /* |
544 | | * Note that output mac is forced to UTF-8... |
545 | | */ |
546 | 0 | if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, |
547 | 0 | EVP_MD_get_type(md_type), prf_md_nid, |
548 | 0 | pkcs12_pbmac1_pbkdf2_key_gen, p12->authsafes->ctx.libctx, p12->authsafes->ctx.propq)) { |
549 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR); |
550 | 0 | goto err; |
551 | 0 | } |
552 | 0 | if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) { |
553 | 0 | ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR); |
554 | 0 | goto err; |
555 | 0 | } |
556 | 0 | ret = 1; |
557 | |
|
558 | 0 | err: |
559 | 0 | X509_ALGOR_free(alg); |
560 | 0 | X509_ALGOR_free(hmac_alg); |
561 | 0 | PBMAC1PARAM_free(param); |
562 | 0 | OPENSSL_free(known_salt); |
563 | 0 | return ret; |
564 | 0 | } |