2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-28 23:17:34 +00:00
|
|
|
*
|
2016-05-17 18:52:22 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* 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 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/pkcs12.h>
|
1999-03-28 23:17:34 +00:00
|
|
|
|
|
|
|
/* Define this to dump decrypted output to files called DERnnn */
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
* #define OPENSSL_DEBUG_DECRYPT
|
2015-01-22 03:40:55 +00:00
|
|
|
*/
|
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
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *PKCS12_pbe_crypt(X509_ALGOR *algor, const char *pass,
|
|
|
|
int passlen, unsigned char *in, int inlen,
|
|
|
|
unsigned char **data, int *datalen, int en_de)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-05-21 00:15:51 +00:00
|
|
|
unsigned char *out = NULL;
|
2015-01-22 03:40:55 +00:00
|
|
|
int outlen, i;
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/* Decrypt data */
|
|
|
|
if (!EVP_PBE_CipherInit(algor->algorithm, pass, passlen,
|
2015-12-13 21:08:41 +00:00
|
|
|
algor->parameter, ctx, en_de)) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT,
|
|
|
|
PKCS12_R_PKCS12_ALGOR_CIPHERINIT_ERROR);
|
2015-05-21 00:15:51 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-13 21:08:41 +00:00
|
|
|
if ((out = OPENSSL_malloc(inlen + EVP_CIPHER_CTX_block_size(ctx)))
|
2015-05-06 17:43:59 +00:00
|
|
|
== NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
PKCS12err(PKCS12_F_PKCS12_PBE_CRYPT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2015-12-13 21:08:41 +00: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 21:08:41 +00: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;
|
|
|
|
if (datalen)
|
|
|
|
*datalen = outlen;
|
|
|
|
if (data)
|
|
|
|
*data = out;
|
|
|
|
err:
|
2015-12-13 21:08:41 +00: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
|
|
|
*/
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
void *PKCS12_item_decrypt_d2i(X509_ALGOR *algor, const ASN1_ITEM *it,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
ASN1_OCTET_STRING *oct, int zbuf)
|
1999-03-28 23:17:34 +00:00
|
|
|
{
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char *out;
|
|
|
|
const unsigned char *p;
|
|
|
|
void *ret;
|
|
|
|
int outlen;
|
|
|
|
|
|
|
|
if (!PKCS12_pbe_crypt(algor, pass, passlen, oct->data, oct->length,
|
|
|
|
&out, &outlen, 0)) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_ITEM_DECRYPT_D2I,
|
|
|
|
PKCS12_R_PKCS12_PBE_CRYPT_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = out;
|
Remove outdated DEBUG flags.
Add -DBIO_DEBUG to --strict-warnings.
Remove comments about outdated debugging ifdef guards.
Remove md_rand ifdef guarding an assert; it doesn't seem used.
Remove the conf guards in conf_api since we use OPENSSL_assert, not assert.
For pkcs12 stuff put OPENSSL_ in front of the macro name.
Merge TLS_DEBUG into SSL_DEBUG.
Various things just turned on/off asserts, mainly for checking non-NULL
arguments, which is now removed: camellia, bn_ctx, crypto/modes.
Remove some old debug code, that basically just printed things to stderr:
DEBUG_PRINT_UNKNOWN_CIPHERSUITES, DEBUG_ZLIB, OPENSSL_RI_DEBUG,
RL_DEBUG, RSA_DEBUG, SCRYPT_DEBUG.
Remove OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL.
Reviewed-by: Richard Levitte <levitte@openssl.org>
2016-02-18 16:33:21 +00:00
|
|
|
#ifdef OPENSSL_DEBUG_DECRYPT
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
FILE *op;
|
|
|
|
|
|
|
|
char fname[30];
|
|
|
|
static int fnm = 1;
|
|
|
|
sprintf(fname, "DER%d", fnm++);
|
|
|
|
op = fopen(fname, "wb");
|
|
|
|
fwrite(p, 1, outlen, op);
|
|
|
|
fclose(op);
|
|
|
|
}
|
1999-03-28 23:17:34 +00:00
|
|
|
#endif
|
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 17:43:59 +00: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 15:30:03 +00:00
|
|
|
ASN1_OCTET_STRING_free(oct);
|
2015-01-22 03:40:55 +00:00
|
|
|
return NULL;
|
1999-03-28 23:17:34 +00:00
|
|
|
}
|