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