Coverage Report

Created: 2025-06-13 06:58

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