]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/pkcs12/p12_decr.c
New GOST PKCS12 standard support
[thirdparty/openssl.git] / crypto / pkcs12 / p12_decr.c
1 /*
2 * Copyright 1999-2016 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 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, 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 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
38 PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR);
39 goto err;
40 }
41
42 /*
43 * GOST algorithm specifics:
44 * OMAC algorithm calculate and encrypt MAC of the encrypted objects
45 * It's appended to encrypted text on encrypting
46 * MAC should be processed on decrypting separately from plain text
47 */
48 max_out_len = inlen + EVP_CIPHER_CTX_block_size(ctx);
49 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
50 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) {
51 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_INTERNAL_ERROR);
52 goto err;
53 }
54
55 if (EVP_CIPHER_CTX_encrypting(ctx)) {
56 max_out_len += mac_len;
57 } else {
58 if (inlen < mac_len) {
59 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
60 PKCS12_R_UNSUPPORTED_PKCS12_MODE);
61 goto err;
62 }
63 inlen -= mac_len;
64 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
65 (int)mac_len, (unsigned char *)in+inlen) < 0) {
66 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_INTERNAL_ERROR);
67 goto err;
68 }
69 }
70 }
71
72 if ((out = OPENSSL_malloc(max_out_len)) == NULL) {
73 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, 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 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, 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 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
89 PKCS12_R_PKCS12_CIPHERFINAL_ERROR);
90 goto err;
91 }
92 outlen += i;
93 if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
94 if (EVP_CIPHER_CTX_encrypting(ctx)) {
95 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
96 (int)mac_len, out+outlen) < 0) {
97 PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_INTERNAL_ERROR);
98 goto err;
99 }
100 outlen += mac_len;
101 }
102 }
103 if (datalen)
104 *datalen = outlen;
105 if (data)
106 *data = out;
107 err:
108 EVP_CIPHER_CTX_free(ctx);
109 return out;
110
111 }
112
113 /*
114 * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
115 * after use.
116 */
117
118 void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
119 const char *pass, int passlen,
120 const ASN1_OCTET_STRING *oct, int zbuf)
121 {
122 unsigned char *out = NULL;
123 const unsigned char *p;
124 void *ret;
125 int outlen = 0;
126
127 if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length,
128 &out, &outlen, 0)) {
129 PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I,
130 PKCS12_R_PKCS12_PBE_CRYPT_ERROR);
131 return NULL;
132 }
133 p = out;
134 OSSL_TRACE_BEGIN(PKCS12_DECRYPT) {
135 BIO_printf(trc_out, "\n");
136 BIO_dump(trc_out, out, outlen);
137 BIO_printf(trc_out, "\n");
138 } OSSL_TRACE_END(PKCS12_DECRYPT);
139 ret = ASN1_item_d2i(NULL, &p, outlen, it);
140 if (zbuf)
141 OPENSSL_cleanse(out, outlen);
142 if (!ret)
143 PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I, PKCS12_R_DECODE_ERROR);
144 OPENSSL_free(out);
145 return ret;
146 }
147
148 /*
149 * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
150 * encoding.
151 */
152
153 ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
154 const ASN1_ITEM *it,
155 const char *pass, int passlen,
156 void *obj, int zbuf)
157 {
158 ASN1_OCTET_STRING *oct = NULL;
159 unsigned char *in = NULL;
160 int inlen;
161
162 if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
163 PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, ERR_R_MALLOC_FAILURE);
164 goto err;
165 }
166 inlen = ASN1_item_i2d(obj, &in, it);
167 if (!in) {
168 PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCODE_ERROR);
169 goto err;
170 }
171 if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data,
172 &oct->length, 1)) {
173 PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCRYPT_ERROR);
174 OPENSSL_free(in);
175 goto err;
176 }
177 if (zbuf)
178 OPENSSL_cleanse(in, inlen);
179 OPENSSL_free(in);
180 return oct;
181 err:
182 ASN1_OCTET_STRING_free(oct);
183 return NULL;
184 }