Coverage Report

Created: 2025-12-14 06:48

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