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