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