2 * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
13 #include <openssl/trace.h>
16 * Encrypt/Decrypt a buffer based on password and algor, result in a
17 * OPENSSL_malloc'ed buffer
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)
25 unsigned char *out = NULL;
27 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
28 int max_out_len, mac_len = 0;
31 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
36 if (!EVP_PBE_CipherInit_ex(algor->algorithm, pass, passlen,
37 algor->parameter, ctx, en_de, libctx, propq))
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
46 max_out_len = inlen + EVP_CIPHER_CTX_block_size(ctx);
47 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
48 & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
49 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) {
50 ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
54 if (EVP_CIPHER_CTX_encrypting(ctx)) {
55 max_out_len += mac_len;
57 if (inlen < mac_len) {
58 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
62 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
63 (int)mac_len, (unsigned char *)in+inlen) < 0) {
64 ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
70 if ((out = OPENSSL_malloc(max_out_len)) == NULL) {
71 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
75 if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
78 ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
83 if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
86 ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PKCS12_CIPHERFINAL_ERROR,
87 passlen == 0 ? "empty password"
88 : "maybe wrong password");
92 if ((EVP_CIPHER_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
93 & EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
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 ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
108 EVP_CIPHER_CTX_free(ctx);
113 unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
114 const char *pass, int passlen,
115 const unsigned char *in, int inlen,
116 unsigned char **data, int *datalen, int en_de)
118 return PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, data, datalen,
123 * Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
127 void *PKCS12_item_decrypt_d2i_ex(const X509_ALGOR *algor, const ASN1_ITEM *it,
128 const char *pass, int passlen,
129 const ASN1_OCTET_STRING *oct, int zbuf,
130 OSSL_LIB_CTX *libctx,
133 unsigned char *out = NULL;
134 const unsigned char *p;
138 if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, oct->data, oct->length,
139 &out, &outlen, 0, libctx, propq))
142 OSSL_TRACE_BEGIN(PKCS12_DECRYPT) {
143 BIO_printf(trc_out, "\n");
144 BIO_dump(trc_out, out, outlen);
145 BIO_printf(trc_out, "\n");
146 } OSSL_TRACE_END(PKCS12_DECRYPT);
147 ret = ASN1_item_d2i(NULL, &p, outlen, it);
149 OPENSSL_cleanse(out, outlen);
151 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
156 void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
157 const char *pass, int passlen,
158 const ASN1_OCTET_STRING *oct, int zbuf)
160 return PKCS12_item_decrypt_d2i_ex(algor, it, pass, passlen, oct, zbuf,
165 * Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
169 ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor,
171 const char *pass, int passlen,
176 ASN1_OCTET_STRING *oct = NULL;
177 unsigned char *in = NULL;
180 if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
181 ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE);
184 inlen = ASN1_item_i2d(obj, &in, it);
186 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
189 if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, &oct->data,
190 &oct->length, 1, ctx, propq)) {
191 ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
196 OPENSSL_cleanse(in, inlen);
200 ASN1_OCTET_STRING_free(oct);
204 ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
206 const char *pass, int passlen,
209 return PKCS12_item_i2d_encrypt_ex(algor, it, pass, passlen, obj, zbuf, NULL, NULL);