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