Coverage Report

Created: 2025-06-13 06:58

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