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_crpt.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2021 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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/core.h>
13
#include <openssl/core_names.h>
14
#include "crypto/evp.h"
15
#include <openssl/pkcs12.h>
16
17
#include <crypto/asn1.h>
18
19
/* PKCS#12 PBE algorithms now in static table */
20
21
void PKCS12_PBE_add(void)
22
0
{
23
0
}
24
25
int PKCS12_PBE_keyivgen_ex(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
26
    ASN1_TYPE *param, const EVP_CIPHER *cipher,
27
    const EVP_MD *md, int en_de,
28
    OSSL_LIB_CTX *libctx, const char *propq)
29
0
{
30
0
    PBEPARAM *pbe;
31
0
    int saltlen, iter, ret;
32
0
    unsigned char *salt;
33
0
    unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
34
0
    unsigned char *piv = iv;
35
36
0
    if (cipher == NULL)
37
0
        return 0;
38
39
    /* Extract useful info from parameter */
40
41
0
    pbe = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(PBEPARAM), param);
42
0
    if (pbe == NULL) {
43
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
44
0
        return 0;
45
0
    }
46
47
0
    if (pbe->iter == NULL)
48
0
        iter = 1;
49
0
    else
50
0
        iter = ASN1_INTEGER_get(pbe->iter);
51
0
    salt = pbe->salt->data;
52
0
    saltlen = pbe->salt->length;
53
0
    if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_KEY_ID,
54
0
            iter, EVP_CIPHER_get_key_length(cipher),
55
0
            key, md,
56
0
            libctx, propq)) {
57
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_KEY_GEN_ERROR);
58
0
        PBEPARAM_free(pbe);
59
0
        return 0;
60
0
    }
61
0
    if (EVP_CIPHER_get_iv_length(cipher) > 0) {
62
0
        if (!PKCS12_key_gen_utf8_ex(pass, passlen, salt, saltlen, PKCS12_IV_ID,
63
0
                iter, EVP_CIPHER_get_iv_length(cipher),
64
0
                iv, md,
65
0
                libctx, propq)) {
66
0
            ERR_raise(ERR_LIB_PKCS12, PKCS12_R_IV_GEN_ERROR);
67
0
            PBEPARAM_free(pbe);
68
0
            return 0;
69
0
        }
70
0
    } else {
71
0
        piv = NULL;
72
0
    }
73
0
    PBEPARAM_free(pbe);
74
0
    ret = EVP_CipherInit_ex(ctx, cipher, NULL, key, piv, en_de);
75
0
    OPENSSL_cleanse(key, EVP_MAX_KEY_LENGTH);
76
0
    OPENSSL_cleanse(iv, EVP_MAX_IV_LENGTH);
77
0
    return ret;
78
0
}
79
80
int PKCS12_PBE_keyivgen(EVP_CIPHER_CTX *ctx, const char *pass, int passlen,
81
    ASN1_TYPE *param, const EVP_CIPHER *cipher,
82
    const EVP_MD *md, int en_de)
83
0
{
84
0
    return PKCS12_PBE_keyivgen_ex(ctx, pass, passlen, param, cipher, md, en_de,
85
0
        NULL, NULL);
86
0
}