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