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