2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2020-10-15 14:10:06 +01:00
|
|
|
* Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-28 23:17:34 +00:00
|
|
|
*
|
2018-12-06 13:49:51 +01:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 14:52:22 -04:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-03-28 23:17:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 16:56:48 +02:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/pkcs12.h>
|
2018-12-13 12:04:26 +01:00
|
|
|
#include <openssl/trace.h>
|
1999-03-28 23:17:34 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Encrypt/Decrypt a buffer based on password and algor, result in a
|
2000-06-01 22:19:21 +00:00
|
|
|
* OPENSSL_malloc'ed buffer
|
1999-03-28 23:17:34 +00:00
|
|
|
*/
|
2016-08-17 17:27:05 +01:00
|
|
|
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
const unsigned char *in, int inlen,
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char **data, int *datalen, int en_de)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-05-21 01:15:51 +01:00
|
|
|
unsigned char *out = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
int outlen, i;
|
2015-12-13 22:08:41 +01:00
|
|
|
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
2020-09-03 16:47:19 +03:00
|
|
|
int max_out_len, mac_len = 0;
|
2015-12-13 22:08:41 +01:00
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2020-09-03 16:47:19 +03:00
|
|
|
/* Process data */
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,
|
2020-09-16 12:52:09 +02:00
|
|
|
algor->parameter, ctx, en_de))
|
2015-05-21 01:15:51 +01:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2020-09-03 16:47:19 +03:00
|
|
|
/*
|
|
|
|
* GOST algorithm specifics:
|
|
|
|
* OMAC algorithm calculate and encrypt MAC of the encrypted objects
|
|
|
|
* It's appended to encrypted text on encrypting
|
|
|
|
* MAC should be processed on decrypting separately from plain text
|
|
|
|
*/
|
|
|
|
max_out_len = inlen + EVP_CIPHER_CTX_block_size(ctx);
|
|
|
|
if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
|
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
|
|
|
max_out_len += mac_len;
|
|
|
|
} else {
|
|
|
|
if (inlen < mac_len) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
|
|
|
|
PKCS12_R_UNSUPPORTED_PKCS12_MODE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
inlen -= mac_len;
|
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
|
|
|
|
(int)mac_len, (unsigned char *)in+inlen) < 0) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((out = OPENSSL_malloc(max_out_len)) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-12-13 22:08:41 +01:00
|
|
|
if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_free(out);
|
|
|
|
out = NULL;
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_EVP_LIB);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen = i;
|
2015-12-13 22:08:41 +01:00
|
|
|
if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
OPENSSL_free(out);
|
|
|
|
out = NULL;
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
|
|
|
|
PKCS12_R_PKCS12_CIPHERFINAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
outlen += i;
|
2020-09-03 16:47:19 +03:00
|
|
|
if (EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_FLAG_CIPHER_WITH_MAC) {
|
|
|
|
if (EVP_CIPHER_CTX_encrypting(ctx)) {
|
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
|
|
|
|
(int)mac_len, out+outlen) < 0) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_INTERNAL_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
outlen += mac_len;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
if (datalen)
|
|
|
|
*datalen = outlen;
|
|
|
|
if (data)
|
|
|
|
*data = out;
|
|
|
|
err:
|
2015-12-13 22:08:41 +01:00
|
|
|
EVP_CIPHER_CTX_free(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
return out;
|
1999-03-28 23:17:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
|
|
|
|
* after use.
|
1999-03-28 23:17:34 +00:00
|
|
|
*/
|
|
|
|
|
2016-08-17 17:27:05 +01:00
|
|
|
void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
|
2015-01-22 03:40:55 +00:00
|
|
|
const char *pass, int passlen,
|
2016-08-17 17:27:05 +01:00
|
|
|
const ASN1_OCTET_STRING *oct, int zbuf)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2020-09-03 16:47:19 +03:00
|
|
|
unsigned char *out = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *p;
|
|
|
|
void *ret;
|
2020-09-03 16:47:19 +03:00
|
|
|
int outlen = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length,
|
2020-09-16 12:52:09 +02:00
|
|
|
&out, &outlen, 0))
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
|
|
|
p = out;
|
2018-12-13 12:04:26 +01:00
|
|
|
OSSL_TRACE_BEGIN(PKCS12_DECRYPT) {
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
BIO_dump(trc_out, out, outlen);
|
|
|
|
BIO_printf(trc_out, "\n");
|
|
|
|
} OSSL_TRACE_END(PKCS12_DECRYPT);
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = ASN1_item_d2i(NULL, &p, outlen, it);
|
|
|
|
if (zbuf)
|
|
|
|
OPENSSL_cleanse(out, outlen);
|
|
|
|
if (!ret)
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I, PKCS12_R_DECODE_ERROR);
|
|
|
|
OPENSSL_free(out);
|
|
|
|
return ret;
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
|
|
|
|
* encoding.
|
1999-03-28 23:17:34 +00:00
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
|
|
|
|
const ASN1_ITEM *it,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
void *obj, int zbuf)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_OCTET_STRING *oct = NULL;
|
|
|
|
unsigned char *in = NULL;
|
|
|
|
int inlen;
|
2015-05-06 13:43:59 -04:00
|
|
|
|
|
|
|
if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
inlen = ASN1_item_i2d(obj, &in, it);
|
|
|
|
if (!in) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCODE_ERROR);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!PKCS12_pbe_crypt(algor, pass, passlen, in, inlen, &oct->data,
|
|
|
|
&oct->length, 1)) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_ITEM_I2D_ENCRYPT, PKCS12_R_ENCRYPT_ERROR);
|
|
|
|
OPENSSL_free(in);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (zbuf)
|
|
|
|
OPENSSL_cleanse(in, inlen);
|
|
|
|
OPENSSL_free(in);
|
|
|
|
return oct;
|
|
|
|
err:
|
2015-04-30 11:30:03 -04:00
|
|
|
ASN1_OCTET_STRING_free(oct);
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|