Coverage Report

Created: 2026-08-18 07:24

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