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