Coverage Report

Created: 2026-09-12 06:55

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
638
{
25
638
    unsigned char *out = NULL;
26
638
    int outlen, i;
27
638
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
28
638
    int max_out_len, mac_len = 0;
29
638
    int block_size;
30
31
638
    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
638
    if (!EVP_PBE_CipherInit_ex(algor->algorithm, pass, passlen,
38
638
            algor->parameter, ctx, en_de, libctx, propq))
39
302
        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
336
    block_size = EVP_CIPHER_CTX_get_block_size(ctx);
48
49
336
    if (block_size == 0) {
50
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
51
0
        goto err;
52
0
    }
53
54
336
    max_out_len = inlen + block_size;
55
336
    if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
56
336
            & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
57
336
        != 0) {
58
0
        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len)
59
0
            <= 0) {
60
0
            ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
61
0
            goto err;
62
0
        }
63
64
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
65
0
            max_out_len += mac_len;
66
0
        } else {
67
0
            if (inlen < mac_len) {
68
0
                ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
69
0
                goto err;
70
0
            }
71
0
            inlen -= mac_len;
72
0
            if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
73
0
                    (int)mac_len, (unsigned char *)in + inlen)
74
0
                <= 0) {
75
0
                ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
76
0
                goto err;
77
0
            }
78
0
        }
79
0
    }
80
81
336
    if ((out = OPENSSL_zalloc(max_out_len)) == NULL)
82
0
        goto err;
83
84
336
    if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
85
72
        OPENSSL_free(out);
86
72
        out = NULL;
87
72
        ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
88
72
        goto err;
89
72
    }
90
91
264
    outlen = i;
92
264
    if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
93
134
        OPENSSL_free(out);
94
134
        out = NULL;
95
134
        ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PKCS12_CIPHERFINAL_ERROR,
96
134
            passlen == 0 ? "empty password"
97
134
                         : "maybe wrong password");
98
134
        goto err;
99
134
    }
100
130
    outlen += i;
101
130
    if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
102
130
            & EVP_CIPH_FLAG_CIPHER_WITH_MAC)
103
130
        != 0) {
104
0
        if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
105
0
            if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
106
0
                    (int)mac_len, out + outlen)
107
0
                <= 0) {
108
0
                OPENSSL_free(out);
109
0
                out = NULL;
110
0
                ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
111
0
                goto err;
112
0
            }
113
0
            outlen += mac_len;
114
0
        }
115
0
    }
116
130
    if (datalen)
117
130
        *datalen = outlen;
118
130
    if (data)
119
130
        *data = out;
120
638
err:
121
638
    EVP_CIPHER_CTX_free(ctx);
122
638
    return out;
123
130
}
124
125
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
126
    const char *pass, int passlen,
127
    const unsigned char *in, int inlen,
128
    unsigned char **data, int *datalen, int en_de)
129
0
{
130
0
    return PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, data, datalen,
131
0
        en_de, NULL, NULL);
132
0
}
133
134
/*
135
 * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
136
 * after use.
137
 */
138
139
void *PKCS12_item_decrypt_d2i_ex(const X509_ALGOR *algor, const ASN1_ITEM *it,
140
    const char *pass, int passlen,
141
    const ASN1_OCTET_STRING *oct, int zbuf,
142
    OSSL_LIB_CTX *libctx,
143
    const char *propq)
144
638
{
145
638
    unsigned char *out = NULL;
146
638
    const unsigned char *p;
147
638
    void *ret;
148
638
    int outlen = 0;
149
150
638
    if (oct == NULL) {
151
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_PASSED_NULL_PARAMETER);
152
0
        return NULL;
153
0
    }
154
155
638
    if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, oct->data, oct->length,
156
638
            &out, &outlen, 0, libctx, propq))
157
508
        return NULL;
158
130
    p = out;
159
130
    OSSL_TRACE_BEGIN(PKCS12_DECRYPT)
160
0
    {
161
0
        BIO_printf(trc_out, "\n");
162
0
        BIO_dump(trc_out, out, outlen);
163
0
        BIO_printf(trc_out, "\n");
164
0
    }
165
130
    OSSL_TRACE_END(PKCS12_DECRYPT);
166
130
    ret = ASN1_item_d2i(NULL, &p, outlen, it);
167
130
    if (zbuf)
168
130
        OPENSSL_cleanse(out, outlen);
169
130
    if (!ret)
170
130
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
171
130
    OPENSSL_free(out);
172
130
    return ret;
173
638
}
174
175
void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
176
    const char *pass, int passlen,
177
    const ASN1_OCTET_STRING *oct, int zbuf)
178
0
{
179
0
    return PKCS12_item_decrypt_d2i_ex(algor, it, pass, passlen, oct, zbuf,
180
0
        NULL, NULL);
181
0
}
182
183
/*
184
 * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
185
 * encoding.
186
 */
187
188
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor,
189
    const ASN1_ITEM *it,
190
    const char *pass, int passlen,
191
    void *obj, int zbuf,
192
    OSSL_LIB_CTX *ctx,
193
    const char *propq)
194
0
{
195
0
    ASN1_OCTET_STRING *oct = NULL;
196
0
    unsigned char *in = NULL;
197
0
    int inlen;
198
199
0
    if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
200
0
        ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
201
0
        goto err;
202
0
    }
203
0
    inlen = ASN1_item_i2d(obj, &in, it);
204
0
    if (!in) {
205
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
206
0
        goto err;
207
0
    }
208
0
    if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, &oct->data,
209
0
            &oct->length, 1, ctx, propq)) {
210
0
        ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
211
0
        OPENSSL_free(in);
212
0
        goto err;
213
0
    }
214
0
    if (zbuf)
215
0
        OPENSSL_cleanse(in, inlen);
216
0
    OPENSSL_free(in);
217
0
    return oct;
218
0
err:
219
0
    ASN1_OCTET_STRING_free(oct);
220
0
    return NULL;
221
0
}
222
223
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
224
    const ASN1_ITEM *it,
225
    const char *pass, int passlen,
226
    void *obj, int zbuf)
227
0
{
228
0
    return PKCS12_item_i2d_encrypt_ex(algor, it, pass, passlen, obj, zbuf, NULL, NULL);
229
0
}