Coverage Report

Created: 2026-07-23 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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.44k
{
33
2.44k
    return p12->mac ? 1 : 0;
34
2.44k
}
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
176
{
84
176
    PBMAC1PARAM *param = NULL;
85
176
    PBKDF2PARAM *pbkdf2_param = NULL;
86
176
    const ASN1_OBJECT *kdf_oid;
87
88
176
    param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter);
89
176
    if (param == NULL) {
90
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT);
91
0
        return NULL;
92
0
    }
93
94
176
    X509_ALGOR_get0(&kdf_oid, NULL, NULL, param->keyDerivationFunc);
95
176
    if (OBJ_obj2nid(kdf_oid) != NID_id_pbkdf2) {
96
6
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT);
97
6
        PBMAC1PARAM_free(param);
98
6
        return NULL;
99
6
    }
100
101
170
    pbkdf2_param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM),
102
170
        param->keyDerivationFunc->parameter);
103
170
    PBMAC1PARAM_free(param);
104
105
170
    return pbkdf2_param;
106
176
}
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
176
{
112
176
    PBKDF2PARAM *pbkdf2_param = NULL;
113
176
    const ASN1_OBJECT *kdf_hmac_oid;
114
176
    int kdf_hmac_nid;
115
176
    int ret = -1;
116
176
    int keylen = 0;
117
176
    EVP_MD *kdf_md = NULL;
118
176
    const ASN1_OCTET_STRING *pbkdf2_salt = NULL;
119
120
176
    pbkdf2_param = PBMAC1_get1_pbkdf2_param(macalg);
121
176
    if (pbkdf2_param == NULL) {
122
12
        ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED);
123
12
        goto err;
124
12
    }
125
126
164
    if (pbkdf2_param->prf == NULL) {
127
86
        kdf_hmac_nid = NID_hmacWithSHA1;
128
86
    } 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
164
    kdf_md = EVP_MD_fetch(ctx, OBJ_nid2sn(ossl_hmac2mdnid(kdf_hmac_nid)), propq);
134
164
    if (kdf_md == NULL) {
135
6
        ERR_raise(ERR_LIB_PKCS12, ERR_R_FETCH_FAILED);
136
6
        goto err;
137
6
    }
138
139
    /* Validate salt is an OCTET STRING choice */
140
158
    if (pbkdf2_param->salt == NULL
141
158
        || 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
140
    pbkdf2_salt = pbkdf2_param->salt->value.octet_string;
146
147
    /* RFC 9879 specifies missing key length as invalid */
148
140
    if (pbkdf2_param->keylength != NULL)
149
128
        keylen = ASN1_INTEGER_get(pbkdf2_param->keylength);
150
    /* RFC 9879 specifies too short key length as untrustworthy too */
151
140
    if (keylen < 20 || keylen > EVP_MAX_MD_SIZE) {
152
36
        ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR,
153
36
            "Invalid Key length (%d is not in the range 20..64)", keylen);
154
36
        goto err;
155
36
    }
156
157
104
    if (PKCS5_PBKDF2_HMAC(pass, passlen, pbkdf2_salt->data, pbkdf2_salt->length,
158
104
            ASN1_INTEGER_get(pbkdf2_param->iter), kdf_md, keylen, key)
159
104
        <= 0) {
160
12
        ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
161
12
        goto err;
162
12
    }
163
92
    ret = keylen;
164
165
176
err:
166
176
    EVP_MD_free(kdf_md);
167
176
    PBKDF2PARAM_free(pbkdf2_param);
168
169
176
    return ret;
170
92
}
171
172
/* Generate a MAC, also used for verification */
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
305
int PKCS12_gen_mac(PKCS12 *p12, const char *pass, int passlen,
306
    unsigned char *mac, unsigned int *maclen)
307
0
{
308
0
    return pkcs12_gen_mac(p12, pass, passlen, mac, maclen, NID_undef, NID_undef, NULL);
309
0
}
310
311
/* Verify the mac */
312
int PKCS12_verify_mac(PKCS12 *p12, const char *pass, int passlen)
313
4.72k
{
314
4.72k
    unsigned char mac[EVP_MAX_MD_SIZE];
315
4.72k
    unsigned int maclen;
316
4.72k
    const ASN1_OCTET_STRING *macoct;
317
4.72k
    const X509_ALGOR *macalg;
318
4.72k
    const ASN1_OBJECT *macoid;
319
320
4.72k
    if (p12->mac == NULL) {
321
1.30k
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT);
322
1.30k
        return 0;
323
1.30k
    }
324
325
3.42k
    X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
326
3.42k
    X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
327
3.42k
    if (OBJ_obj2nid(macoid) == NID_pbmac1) {
328
200
        PBMAC1PARAM *param = NULL;
329
200
        const ASN1_OBJECT *hmac_oid;
330
200
        int md_nid = NID_undef;
331
332
200
        param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter);
333
200
        if (param == NULL) {
334
18
            ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED);
335
18
            return 0;
336
18
        }
337
182
        X509_ALGOR_get0(&hmac_oid, NULL, NULL, param->messageAuthScheme);
338
182
        md_nid = ossl_hmac2mdnid(OBJ_obj2nid(hmac_oid));
339
340
182
        if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, md_nid, NID_undef, NULL)) {
341
90
            ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
342
90
            PBMAC1PARAM_free(param);
343
90
            return 0;
344
90
        }
345
92
        PBMAC1PARAM_free(param);
346
3.22k
    } else {
347
3.22k
        if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NID_undef, NID_undef, NULL)) {
348
2.25k
            ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
349
2.25k
            return 0;
350
2.25k
        }
351
3.22k
    }
352
1.05k
    X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
353
1.05k
    if ((maclen != (unsigned int)ASN1_STRING_length(macoct))
354
681
        || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
355
1.02k
        return 0;
356
357
32
    return 1;
358
1.05k
}
359
360
/* Set a mac */
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
0
{
365
0
    unsigned char mac[EVP_MAX_MD_SIZE];
366
0
    unsigned int maclen;
367
0
    ASN1_OCTET_STRING *macoct;
368
369
0
    if (md_type == NULL)
370
        /* No need to do a fetch as the md_type is used only to get a NID */
371
0
        md_type = EVP_sha256();
372
0
    if (!iter)
373
0
        iter = PKCS12_DEFAULT_ITER;
374
0
    if (PKCS12_setup_mac(p12, iter, salt, saltlen, md_type) == PKCS12_ERROR) {
375
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR);
376
0
        return 0;
377
0
    }
378
    /*
379
     * Note that output mac is forced to UTF-8...
380
     */
381
0
    if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NID_undef, NID_undef, NULL)) {
382
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
383
0
        return 0;
384
0
    }
385
0
    X509_SIG_getm(p12->mac->dinfo, NULL, &macoct);
386
0
    if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
387
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR);
388
0
        return 0;
389
0
    }
390
0
    return 1;
391
0
}
392
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
0
{
399
0
    return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter,
400
0
        md_type, keylen, out);
401
0
}
402
403
static int pkcs12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
404
    int nid)
405
0
{
406
0
    X509_ALGOR *macalg;
407
408
0
    PKCS12_MAC_DATA_free(p12->mac);
409
0
    p12->mac = NULL;
410
411
0
    if ((p12->mac = PKCS12_MAC_DATA_new()) == NULL)
412
0
        return PKCS12_ERROR;
413
0
    if (iter > 1) {
414
0
        if ((p12->mac->iter = ASN1_INTEGER_new()) == NULL) {
415
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
416
0
            return 0;
417
0
        }
418
0
        if (!ASN1_INTEGER_set(p12->mac->iter, iter)) {
419
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
420
0
            return 0;
421
0
        }
422
0
    }
423
0
    if (saltlen == 0)
424
0
        saltlen = PKCS12_SALT_LEN;
425
0
    else if (saltlen < 0)
426
0
        return 0;
427
0
    if ((p12->mac->salt->data = OPENSSL_malloc(saltlen)) == NULL)
428
0
        return 0;
429
0
    p12->mac->salt->length = saltlen;
430
0
    if (salt == NULL) {
431
0
        if (RAND_bytes_ex(p12->authsafes->ctx.libctx, p12->mac->salt->data,
432
0
                (size_t)saltlen, 0)
433
0
            <= 0)
434
0
            return 0;
435
0
    } else {
436
0
        memcpy(p12->mac->salt->data, salt, saltlen);
437
0
    }
438
0
    X509_SIG_getm(p12->mac->dinfo, &macalg, NULL);
439
0
    if (!X509_ALGOR_set0(macalg, OBJ_nid2obj(nid), V_ASN1_NULL, NULL)) {
440
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
441
0
        return 0;
442
0
    }
443
444
0
    return 1;
445
0
}
446
447
/* Set up a mac structure */
448
int PKCS12_setup_mac(PKCS12 *p12, int iter, unsigned char *salt, int saltlen,
449
    const EVP_MD *md_type)
450
0
{
451
0
    return pkcs12_setup_mac(p12, iter, salt, saltlen, EVP_MD_get_type(md_type));
452
0
}
453
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
0
{
458
0
    unsigned char mac[EVP_MAX_MD_SIZE];
459
0
    unsigned int maclen;
460
0
    ASN1_OCTET_STRING *macoct;
461
0
    X509_ALGOR *alg = NULL;
462
0
    int ret = 0;
463
0
    int prf_md_nid = NID_undef, prf_nid = NID_undef, hmac_nid;
464
0
    unsigned char *known_salt = NULL;
465
0
    int keylen = 0;
466
0
    PBMAC1PARAM *param = NULL;
467
0
    X509_ALGOR *hmac_alg = NULL, *macalg = NULL;
468
469
0
    if (md_type == NULL)
470
        /* No need to do a fetch as the md_type is used only to get a NID */
471
0
        md_type = EVP_sha256();
472
473
0
    if (prf_md_name == NULL)
474
0
        prf_md_nid = EVP_MD_get_type(md_type);
475
0
    else
476
0
        prf_md_nid = OBJ_txt2nid(prf_md_name);
477
478
0
    if (iter == 0)
479
0
        iter = PKCS12_DEFAULT_ITER;
480
481
0
    keylen = EVP_MD_get_size(md_type);
482
483
0
    prf_nid = ossl_md2hmacnid(prf_md_nid);
484
0
    hmac_nid = ossl_md2hmacnid(EVP_MD_get_type(md_type));
485
486
0
    if (prf_nid == NID_undef || hmac_nid == NID_undef) {
487
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
488
0
        goto err;
489
0
    }
490
491
0
    if (salt == NULL) {
492
0
        known_salt = OPENSSL_malloc(saltlen);
493
0
        if (known_salt == NULL)
494
0
            goto err;
495
496
0
        if (RAND_bytes_ex(NULL, known_salt, saltlen, 0) <= 0) {
497
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_RAND_LIB);
498
0
            goto err;
499
0
        }
500
0
    }
501
502
0
    param = PBMAC1PARAM_new();
503
0
    hmac_alg = X509_ALGOR_new();
504
0
    alg = PKCS5_pbkdf2_set(iter, salt ? salt : known_salt, saltlen, prf_nid, keylen);
505
0
    if (param == NULL || hmac_alg == NULL || alg == NULL)
506
0
        goto err;
507
508
0
    if (pkcs12_setup_mac(p12, iter, salt ? salt : known_salt, saltlen,
509
0
            NID_pbmac1)
510
0
        == PKCS12_ERROR) {
511
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR);
512
0
        goto err;
513
0
    }
514
515
0
    if (!X509_ALGOR_set0(hmac_alg, OBJ_nid2obj(hmac_nid), V_ASN1_NULL, NULL)) {
516
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_SETUP_ERROR);
517
0
        goto err;
518
0
    }
519
520
0
    X509_ALGOR_free(param->keyDerivationFunc);
521
0
    X509_ALGOR_free(param->messageAuthScheme);
522
0
    param->keyDerivationFunc = alg;
523
0
    param->messageAuthScheme = hmac_alg;
524
0
    alg = NULL;
525
0
    hmac_alg = NULL;
526
527
0
    X509_SIG_getm(p12->mac->dinfo, &macalg, &macoct);
528
0
    if (!ASN1_TYPE_pack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), param, &macalg->parameter))
529
0
        goto err;
530
531
    /*
532
     * Note that output mac is forced to UTF-8...
533
     */
534
0
    if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen,
535
0
            EVP_MD_get_type(md_type), prf_md_nid,
536
0
            pkcs12_pbmac1_pbkdf2_key_gen)) {
537
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
538
0
        goto err;
539
0
    }
540
0
    if (!ASN1_OCTET_STRING_set(macoct, mac, maclen)) {
541
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_STRING_SET_ERROR);
542
0
        goto err;
543
0
    }
544
0
    ret = 1;
545
546
0
err:
547
0
    X509_ALGOR_free(alg);
548
0
    X509_ALGOR_free(hmac_alg);
549
0
    PBMAC1PARAM_free(param);
550
0
    OPENSSL_free(known_salt);
551
0
    return ret;
552
0
}