Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/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
5.31k
{
36
5.31k
    return p12->mac ? 1 : 0;
37
5.31k
}
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
590
{
87
590
    PBMAC1PARAM *param = NULL;
88
590
    PBKDF2PARAM *pbkdf2_param = NULL;
89
590
    const ASN1_OBJECT *kdf_oid;
90
91
590
    param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter);
92
590
    if (param == NULL) {
93
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT);
94
0
        return NULL;
95
0
    }
96
97
590
    X509_ALGOR_get0(&kdf_oid, NULL, NULL, param->keyDerivationFunc);
98
590
    if (OBJ_obj2nid(kdf_oid) != NID_id_pbkdf2) {
99
78
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_INVALID_ARGUMENT);
100
78
        PBMAC1PARAM_free(param);
101
78
        return NULL;
102
78
    }
103
104
512
    pbkdf2_param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBKDF2PARAM),
105
512
        param->keyDerivationFunc->parameter);
106
512
    PBMAC1PARAM_free(param);
107
108
512
    return pbkdf2_param;
109
590
}
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
590
{
115
590
    PBKDF2PARAM *pbkdf2_param = NULL;
116
590
    const ASN1_OBJECT *kdf_hmac_oid;
117
590
    int kdf_hmac_nid;
118
590
    int ret = -1;
119
590
    int keylen = 0;
120
590
    EVP_MD *kdf_md = NULL;
121
590
    const ASN1_OCTET_STRING *pbkdf2_salt = NULL;
122
123
590
    pbkdf2_param = PBMAC1_get1_pbkdf2_param(macalg);
124
590
    if (pbkdf2_param == NULL) {
125
180
        ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED);
126
180
        goto err;
127
180
    }
128
129
410
    if (pbkdf2_param->prf == NULL) {
130
116
        kdf_hmac_nid = NID_hmacWithSHA1;
131
294
    } else {
132
294
        X509_ALGOR_get0(&kdf_hmac_oid, NULL, NULL, pbkdf2_param->prf);
133
294
        kdf_hmac_nid = OBJ_obj2nid(kdf_hmac_oid);
134
294
    }
135
136
410
    kdf_md = EVP_MD_fetch(ctx, OBJ_nid2sn(ossl_hmac2mdnid(kdf_hmac_nid)), propq);
137
410
    if (kdf_md == NULL) {
138
24
        ERR_raise(ERR_LIB_PKCS12, ERR_R_FETCH_FAILED);
139
24
        goto err;
140
24
    }
141
142
    /* Validate salt is an OCTET STRING choice */
143
386
    if (pbkdf2_param->salt == NULL
144
386
        || pbkdf2_param->salt->type != V_ASN1_OCTET_STRING) {
145
36
        ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR, "Invalid Salt");
146
36
        goto err;
147
36
    }
148
350
    pbkdf2_salt = pbkdf2_param->salt->value.octet_string;
149
150
    /* RFC 9879 specifies missing key length as invalid */
151
350
    if (pbkdf2_param->keylength != NULL)
152
326
        keylen = ASN1_INTEGER_get(pbkdf2_param->keylength);
153
    /* RFC 9879 specifies too short key length as untrustworthy too */
154
350
    if (keylen < 20 || keylen > EVP_MAX_MD_SIZE) {
155
90
        ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PARSE_ERROR,
156
90
            "Invalid Key length (%d is not in the range 20..64)", keylen);
157
90
        goto err;
158
90
    }
159
160
260
    if (PKCS5_PBKDF2_HMAC(pass, passlen, pbkdf2_salt->data, pbkdf2_salt->length,
161
260
            ASN1_INTEGER_get(pbkdf2_param->iter), kdf_md, keylen, key)
162
260
        <= 0) {
163
30
        ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
164
30
        goto err;
165
30
    }
166
230
    ret = keylen;
167
168
590
err:
169
590
    EVP_MD_free(kdf_md);
170
590
    PBKDF2PARAM_free(pbkdf2_param);
171
172
590
    return ret;
173
230
}
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
8.09k
{
187
8.09k
    int ret = 0;
188
8.09k
    EVP_MD *md;
189
8.09k
    unsigned char key[EVP_MAX_MD_SIZE], *salt;
190
8.09k
    int saltlen, iter;
191
8.09k
    char md_name[80];
192
8.09k
    int keylen = 0;
193
8.09k
    int md_nid = NID_undef;
194
8.09k
    const X509_ALGOR *macalg;
195
8.09k
    const ASN1_OBJECT *macoid;
196
8.09k
    OSSL_LIB_CTX *libctx;
197
8.09k
    const char *propq;
198
8.09k
    size_t md_sz, outlen;
199
200
8.09k
    if (!PKCS7_type_is_data(p12->authsafes)) {
201
84
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA);
202
84
        return 0;
203
84
    }
204
205
8.01k
    if (p12->authsafes->d.data == NULL) {
206
18
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
207
18
        return 0;
208
18
    }
209
210
7.99k
    libctx = p12->authsafes->ctx.libctx;
211
7.99k
    propq = p12->authsafes->ctx.propq;
212
7.99k
    salt = p12->mac->salt->data;
213
7.99k
    saltlen = p12->mac->salt->length;
214
7.99k
    if (p12->mac->iter == NULL)
215
844
        iter = 1;
216
7.14k
    else
217
7.14k
        iter = ASN1_INTEGER_get(p12->mac->iter);
218
7.99k
    X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
219
7.99k
    X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
220
7.99k
    if (OBJ_obj2nid(macoid) == NID_pbmac1) {
221
614
        if (OBJ_obj2txt(md_name, sizeof(md_name), OBJ_nid2obj(pbmac1_md_nid), 0) < 0)
222
0
            return 0;
223
7.37k
    } else {
224
7.37k
        if (OBJ_obj2txt(md_name, sizeof(md_name), macoid, 0) < 0)
225
0
            return 0;
226
7.37k
    }
227
7.99k
    md = EVP_MD_fetch(libctx, md_name, propq);
228
229
7.99k
    if (md == NULL) {
230
4.56k
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNKNOWN_DIGEST_ALGORITHM);
231
4.56k
        return 0;
232
4.56k
    }
233
234
3.42k
    keylen = EVP_MD_get_size(md);
235
3.42k
    md_nid = EVP_MD_get_type(md);
236
3.42k
    if (keylen <= 0)
237
36
        goto err;
238
3.39k
    md_sz = keylen;
239
240
    /* For PBMAC1 we use a special keygen callback if not provided (e.g. on verification) */
241
3.39k
    if (pbmac1_md_nid != NID_undef && pkcs12_key_gen == NULL) {
242
590
        keylen = PBMAC1_PBKDF2_HMAC(libctx, propq, pass, passlen, macalg, key);
243
590
        if (keylen < 0)
244
360
            goto err;
245
2.80k
    } else if ((md_nid == NID_id_GostR3411_94
246
2.80k
                   || md_nid == NID_id_GostR3411_2012_256
247
2.80k
                   || 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
2.80k
    } else {
256
2.80k
        EVP_MD *hmac_md = md;
257
2.80k
        int fetched = 0;
258
259
2.80k
        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
2.80k
        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
2.80k
        } else {
280
2.80k
            if (fetched)
281
0
                EVP_MD_free(hmac_md);
282
            /* Default to UTF-8 password */
283
2.80k
            if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_MAC_ID,
284
2.80k
                    iter, keylen, key, md, libctx, propq)) {
285
885
                ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
286
885
                goto err;
287
885
            }
288
2.80k
        }
289
2.80k
    }
290
2.14k
    if (EVP_Q_mac(libctx, "HMAC", propq, md_name, NULL, key, keylen,
291
2.14k
            p12->authsafes->d.data->data, p12->authsafes->d.data->length,
292
2.14k
            mac, md_sz, &outlen)
293
2.14k
        == NULL)
294
0
        goto err;
295
2.14k
    if (outlen > UINT_MAX)
296
0
        goto err;
297
2.14k
    *maclen = (unsigned int)outlen;
298
2.14k
    ret = 1;
299
3.42k
err:
300
3.42k
    OPENSSL_cleanse(key, sizeof(key));
301
3.42k
    EVP_MD_free(md);
302
3.42k
    return ret;
303
2.14k
}
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
10.9k
{
314
10.9k
    unsigned char mac[EVP_MAX_MD_SIZE];
315
10.9k
    unsigned int maclen;
316
10.9k
    const ASN1_OCTET_STRING *macoct;
317
10.9k
    const X509_ALGOR *macalg;
318
10.9k
    const ASN1_OBJECT *macoid;
319
320
10.9k
    if (p12->mac == NULL) {
321
2.50k
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_ABSENT);
322
2.50k
        return 0;
323
2.50k
    }
324
325
8.40k
    X509_SIG_get0(p12->mac->dinfo, &macalg, NULL);
326
8.40k
    X509_ALGOR_get0(&macoid, NULL, NULL, macalg);
327
8.40k
    if (OBJ_obj2nid(macoid) == NID_pbmac1) {
328
926
        PBMAC1PARAM *param = NULL;
329
926
        const ASN1_OBJECT *hmac_oid;
330
926
        int md_nid = NID_undef;
331
332
926
        param = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBMAC1PARAM), macalg->parameter);
333
926
        if (param == NULL) {
334
312
            ERR_raise(ERR_LIB_PKCS12, ERR_R_UNSUPPORTED);
335
312
            return 0;
336
312
        }
337
614
        X509_ALGOR_get0(&hmac_oid, NULL, NULL, param->messageAuthScheme);
338
614
        md_nid = ossl_hmac2mdnid(OBJ_obj2nid(hmac_oid));
339
340
614
        if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, md_nid, NID_undef, NULL)) {
341
384
            ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
342
384
            PBMAC1PARAM_free(param);
343
384
            return 0;
344
384
        }
345
230
        PBMAC1PARAM_free(param);
346
7.48k
    } else {
347
7.48k
        if (!pkcs12_gen_mac(p12, pass, passlen, mac, &maclen, NID_undef, NID_undef, NULL)) {
348
5.56k
            ERR_raise(ERR_LIB_PKCS12, PKCS12_R_MAC_GENERATION_ERROR);
349
5.56k
            return 0;
350
5.56k
        }
351
7.48k
    }
352
2.14k
    X509_SIG_get0(p12->mac->dinfo, NULL, &macoct);
353
2.14k
    if ((maclen != ASN1_STRING_get_length(macoct))
354
1.19k
        || CRYPTO_memcmp(mac, ASN1_STRING_get0_data(macoct), maclen) != 0)
355
2.06k
        return 0;
356
357
80
    return 1;
358
2.14k
}
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
}