Coverage Report

Created: 2026-03-09 06:55

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