Coverage Report

Created: 2025-12-31 06:58

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