Coverage Report

Created: 2026-02-14 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl34/crypto/pkcs12/p12_decr.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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/pkcs12.h>
13
#include <openssl/trace.h>
14
15
/*
16
 * Encrypt/Decrypt a buffer based on password and algor, result in a
17
 * OPENSSL_malloc'ed buffer
18
 */
19
unsigned char *PKCS12_pbe_crypt_ex(const X509_ALGOR *algor,
20
    const char *pass, int passlen,
21
    const unsigned char *in, int inlen,
22
    unsigned char **data, int *datalen, int en_de,
23
    OSSL_LIB_CTX *libctx, const char *propq)
24
320
{
25
320
    unsigned char *out = NULL;
26
320
    int outlen, i;
27
320
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
28
320
    int max_out_len, mac_len = 0;
29
320
    int block_size;
30
31
320
    if (ctx == NULL) {
32
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
33
0
        goto err;
34
0
    }
35
36
    /* Process data */
37
320
    if (!EVP_PBE_CipherInit_ex(algor->algorithm, pass, passlen,
38
320
            algor->parameter, ctx, en_de, libctx, propq))
39
184
        goto err;
40
41
    /*
42
     * GOST algorithm specifics:
43
     * OMAC algorithm calculate and encrypt MAC of the encrypted objects
44
     * It's appended to encrypted text on encrypting
45
     * MAC should be processed on decrypting separately from plain text
46
     */
47
136
    block_size = EVP_CIPHER_CTX_get_block_size(ctx);
48
49
136
    if (block_size == 0) {
50
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
51
0
        goto err;
52
0
    }
53
54
136
    max_out_len = inlen + block_size;
55
136
    if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
56
136
            & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
57
136
        != 0) {
58
0
        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) {
59
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
60
0
            goto err;
61
0
        }
62
63
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
64
0
            max_out_len += mac_len;
65
0
        } else {
66
0
            if (inlen < mac_len) {
67
0
                ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
68
0
                goto err;
69
0
            }
70
0
            inlen -= mac_len;
71
0
            if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
72
0
                    (int)mac_len, (unsigned char *)in + inlen)
73
0
                < 0) {
74
0
                ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
75
0
                goto err;
76
0
            }
77
0
        }
78
0
    }
79
80
136
    if ((out = OPENSSL_zalloc(max_out_len)) == NULL)
81
0
        goto err;
82
83
136
    if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
84
22
        OPENSSL_free(out);
85
22
        out = NULL;
86
22
        ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
87
22
        goto err;
88
22
    }
89
90
114
    outlen = i;
91
114
    if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
92
54
        OPENSSL_free(out);
93
54
        out = NULL;
94
54
        ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PKCS12_CIPHERFINAL_ERROR,
95
54
            passlen == 0 ? "empty password"
96
54
                         : "maybe wrong password");
97
54
        goto err;
98
54
    }
99
60
    outlen += i;
100
60
    if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
101
60
            & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
102
60
        != 0) {
103
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
104
0
            if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
105
0
                    (int)mac_len, out + outlen)
106
0
                < 0) {
107
0
                OPENSSL_free(out);
108
0
                out = NULL;
109
0
                ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
110
0
                goto err;
111
0
            }
112
0
            outlen += mac_len;
113
0
        }
114
0
    }
115
60
    if (datalen)
116
60
        *datalen = outlen;
117
60
    if (data)
118
60
        *data = out;
119
320
err:
120
320
    EVP_CIPHER_CTX_free(ctx);
121
320
    return out;
122
60
}
123
124
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
125
    const char *pass, int passlen,
126
    const unsigned char *in, int inlen,
127
    unsigned char **data, int *datalen, int en_de)
128
0
{
129
0
    return PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, data, datalen,
130
0
        en_de, NULL, NULL);
131
0
}
132
133
/*
134
 * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
135
 * after use.
136
 */
137
138
void *PKCS12_item_decrypt_d2i_ex(const X509_ALGOR *algor, const ASN1_ITEM *it,
139
    const char *pass, int passlen,
140
    const ASN1_OCTET_STRING *oct, int zbuf,
141
    OSSL_LIB_CTX *libctx,
142
    const char *propq)
143
320
{
144
320
    unsigned char *out = NULL;
145
320
    const unsigned char *p;
146
320
    void *ret;
147
320
    int outlen = 0;
148
149
320
    if (oct == NULL) {
150
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
151
0
        return NULL;
152
0
    }
153
154
320
    if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, oct->data, oct->length,
155
320
            &out, &outlen, 0, libctx, propq))
156
260
        return NULL;
157
60
    p = out;
158
60
    OSSL_TRACE_BEGIN(PKCS12_DECRYPT)
159
0
    {
160
0
        BIO_printf(trc_out, "\n");
161
0
        BIO_dump(trc_out, out, outlen);
162
0
        BIO_printf(trc_out, "\n");
163
0
    }
164
60
    OSSL_TRACE_END(PKCS12_DECRYPT);
165
60
    ret = ASN1_item_d2i(NULL, &p, outlen, it);
166
60
    if (zbuf)
167
60
        OPENSSL_cleanse(out, outlen);
168
60
    if (!ret)
169
60
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
170
60
    OPENSSL_free(out);
171
60
    return ret;
172
320
}
173
174
void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
175
    const char *pass, int passlen,
176
    const ASN1_OCTET_STRING *oct, int zbuf)
177
0
{
178
0
    return PKCS12_item_decrypt_d2i_ex(algor, it, pass, passlen, oct, zbuf,
179
0
        NULL, NULL);
180
0
}
181
182
/*
183
 * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
184
 * encoding.
185
 */
186
187
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor,
188
    const ASN1_ITEM *it,
189
    const char *pass, int passlen,
190
    void *obj, int zbuf,
191
    OSSL_LIB_CTX *ctx,
192
    const char *propq)
193
0
{
194
0
    ASN1_OCTET_STRING *oct = NULL;
195
0
    unsigned char *in = NULL;
196
0
    int inlen;
197
198
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
199
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
200
0
        goto err;
201
0
    }
202
0
    inlen = ASN1_item_i2d(obj, &in, it);
203
0
    if (!in) {
204
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
205
0
        goto err;
206
0
    }
207
0
    if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, &oct->data,
208
0
            &oct->length, 1, ctx, propq)) {
209
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
210
0
        OPENSSL_free(in);
211
0
        goto err;
212
0
    }
213
0
    if (zbuf)
214
0
        OPENSSL_cleanse(in, inlen);
215
0
    OPENSSL_free(in);
216
0
    return oct;
217
0
err:
218
0
    ASN1_OCTET_STRING_free(oct);
219
0
    return NULL;
220
0
}
221
222
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
223
    const ASN1_ITEM *it,
224
    const char *pass, int passlen,
225
    void *obj, int zbuf)
226
0
{
227
0
    return PKCS12_item_i2d_encrypt_ex(algor, it, pass, passlen, obj, zbuf, NULL, NULL);
228
0
}