Coverage Report

Created: 2026-06-18 06:34

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