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