2015-01-22 11:40:55 +08:00
|
|
|
/*
|
2021-03-11 21:27:36 +08:00
|
|
|
* Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-29 07:17:34 +08:00
|
|
|
*
|
2018-12-06 20:49:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08: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-29 07:17:34 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/pkcs12.h>
|
2018-12-13 19:04:26 +08:00
|
|
|
#include <openssl/trace.h>
|
1999-03-29 07:17:34 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Encrypt/Decrypt a buffer based on password and algor, result in a
|
2000-06-02 06:19:21 +08:00
|
|
|
* OPENSSL_malloc'ed buffer
|
1999-03-29 07:17:34 +08:00
|
|
|
*/
|
2021-02-17 15:56:36 +08:00
|
|
|
unsigned char *PKCS12_pbe_crypt_ex(const X509_ALGOR *algor,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
const unsigned char *in, int inlen,
|
|
|
|
unsigned char **data, int *datalen, int en_de,
|
|
|
|
OSSL_LIB_CTX *libctx, const char *propq)
|
1999-03-29 07:17:34 +08:00
|
|
|
{
|
2015-05-21 08:15:51 +08:00
|
|
|
unsigned char *out = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
int outlen, i;
|
2015-12-14 05:08:41 +08:00
|
|
|
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
|
2020-09-03 21:47:19 +08:00
|
|
|
int max_out_len, mac_len = 0;
|
2015-12-14 05:08:41 +08:00
|
|
|
|
|
|
|
if (ctx == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
|
2015-12-14 05:08:41 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-09-03 21:47:19 +08:00
|
|
|
/* Process data */
|
2021-02-17 15:56:36 +08:00
|
|
|
if (!EVP_PBE_CipherInit_ex(algor->algorithm, pass, passlen,
|
|
|
|
algor->parameter, ctx, en_de, libctx, propq))
|
2015-05-21 08:15:51 +08:00
|
|
|
goto err;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2020-09-03 21:47:19 +08: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
|
|
|
|
*/
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
max_out_len = inlen + EVP_CIPHER_CTX_get_block_size(ctx);
|
|
|
|
if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
|
Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new. Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field. The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md(). Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md(). Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
if (flags & x) --> if ((flags & x) != 0)
if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
2021-02-17 06:51:56 +08:00
|
|
|
& EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
|
2020-09-03 21:47:19 +08:00
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_TLS1_AAD, 0, &mac_len) < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
|
2020-09-03 21:47:19 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
|
2020-09-03 21:47:19 +08:00
|
|
|
max_out_len += mac_len;
|
|
|
|
} else {
|
|
|
|
if (inlen < mac_len) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_UNSUPPORTED_PKCS12_MODE);
|
2020-09-03 21:47:19 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
inlen -= mac_len;
|
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
|
|
|
|
(int)mac_len, (unsigned char *)in+inlen) < 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
|
2020-09-03 21:47:19 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((out = OPENSSL_malloc(max_out_len)) == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
|
2015-12-14 05:08:41 +08:00
|
|
|
if (!EVP_CipherUpdate(ctx, out, &i, in, inlen)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(out);
|
|
|
|
out = NULL;
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_EVP_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
outlen = i;
|
2015-12-14 05:08:41 +08:00
|
|
|
if (!EVP_CipherFinal_ex(ctx, out + i, &i)) {
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(out);
|
|
|
|
out = NULL;
|
2020-11-26 15:35:26 +08:00
|
|
|
ERR_raise_data(ERR_LIB_PKCS12, PKCS12_R_PKCS12_CIPHERFINAL_ERROR,
|
|
|
|
passlen == 0 ? "empty password"
|
|
|
|
: "maybe wrong password");
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
outlen += i;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
|
Add "origin" field to EVP_CIPHER, EVP_MD
Add a "where did this EVP_{CIPHER,MD} come from" flag: global, via fetch,
or via EVP_{CIPHER,MD}_meth_new. Update EVP_{CIPHER,MD}_free to handle all
three origins. The flag is deliberately right before some function pointers,
so that compile-time failures (int/pointer) will occur, as opposed to
taking a bit in the existing "flags" field. The "global variable" flag
is non-zero, so the default case of using OPENSSL_zalloc (for provider
ciphers), will do the right thing. Ref-counting is a no-op for
Make up_ref no-op for global MD and CIPHER objects
Deprecate EVP_MD_CTX_md(). Added EVP_MD_CTX_get0_md() (same semantics as
the deprecated function) and EVP_MD_CTX_get1_md(). Likewise, deprecate
EVP_CIPHER_CTX_cipher() in favor of EVP_CIPHER_CTX_get0_cipher(), and add
EVP_CIPHER_CTX_get1_CIPHER().
Refactor EVP_MD_free() and EVP_MD_meth_free() to call new common
evp_md_free_int() function.
Refactor EVP_CIPHER_free() and EVP_CIPHER_meth_free() to call new common
evp_cipher_free_int() function.
Also change some flags tests to explicit test == or != zero. E.g.,
if (flags & x) --> if ((flags & x) != 0)
if (!(flags & x)) --> if ((flags & x) == 0)
Only done for those lines where "get0_cipher" calls were made.
Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14193)
2021-02-17 06:51:56 +08:00
|
|
|
& EVP_CIPH_FLAG_CIPHER_WITH_MAC) != 0) {
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
if (EVP_CIPHER_CTX_is_encrypting(ctx)) {
|
2020-09-03 21:47:19 +08:00
|
|
|
if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
|
|
|
|
(int)mac_len, out+outlen) < 0) {
|
2022-07-15 17:21:30 +08:00
|
|
|
OPENSSL_free(out);
|
|
|
|
out = NULL;
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_INTERNAL_ERROR);
|
2020-09-03 21:47:19 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
outlen += mac_len;
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 11:40:55 +08:00
|
|
|
if (datalen)
|
|
|
|
*datalen = outlen;
|
|
|
|
if (data)
|
|
|
|
*data = out;
|
|
|
|
err:
|
2015-12-14 05:08:41 +08:00
|
|
|
EVP_CIPHER_CTX_free(ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
return out;
|
1999-03-29 07:17:34 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
unsigned char *PKCS12_pbe_crypt(const X509_ALGOR *algor,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
const unsigned char *in, int inlen,
|
|
|
|
unsigned char **data, int *datalen, int en_de)
|
|
|
|
{
|
|
|
|
return PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, data, datalen,
|
|
|
|
en_de, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Decrypt an OCTET STRING and decode ASN1 structure if zbuf set zero buffer
|
|
|
|
* after use.
|
1999-03-29 07:17:34 +08:00
|
|
|
*/
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
void *PKCS12_item_decrypt_d2i_ex(const X509_ALGOR *algor, const ASN1_ITEM *it,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
const ASN1_OCTET_STRING *oct, int zbuf,
|
|
|
|
OSSL_LIB_CTX *libctx,
|
|
|
|
const char *propq)
|
1999-03-29 07:17:34 +08:00
|
|
|
{
|
2020-09-03 21:47:19 +08:00
|
|
|
unsigned char *out = NULL;
|
2015-01-22 11:40:55 +08:00
|
|
|
const unsigned char *p;
|
|
|
|
void *ret;
|
2020-09-03 21:47:19 +08:00
|
|
|
int outlen = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, oct->data, oct->length,
|
|
|
|
&out, &outlen, 0, libctx, propq))
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
p = out;
|
2018-12-13 19:04:26 +08: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 11:40:55 +08:00
|
|
|
ret = ASN1_item_d2i(NULL, &p, outlen, it);
|
|
|
|
if (zbuf)
|
|
|
|
OPENSSL_cleanse(out, outlen);
|
|
|
|
if (!ret)
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_DECODE_ERROR);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(out);
|
|
|
|
return ret;
|
1999-03-29 07:17:34 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
void *PKCS12_item_decrypt_d2i(const X509_ALGOR *algor, const ASN1_ITEM *it,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
const ASN1_OCTET_STRING *oct, int zbuf)
|
|
|
|
{
|
|
|
|
return PKCS12_item_decrypt_d2i_ex(algor, it, pass, passlen, oct, zbuf,
|
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* Encode ASN1 structure and encrypt, return OCTET STRING if zbuf set zero
|
|
|
|
* encoding.
|
1999-03-29 07:17:34 +08:00
|
|
|
*/
|
|
|
|
|
2021-02-17 15:56:36 +08:00
|
|
|
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt_ex(X509_ALGOR *algor,
|
|
|
|
const ASN1_ITEM *it,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
void *obj, int zbuf,
|
|
|
|
OSSL_LIB_CTX *ctx,
|
|
|
|
const char *propq)
|
1999-03-29 07:17:34 +08:00
|
|
|
{
|
2015-01-22 11:40:55 +08:00
|
|
|
ASN1_OCTET_STRING *oct = NULL;
|
|
|
|
unsigned char *in = NULL;
|
|
|
|
int inlen;
|
2015-05-07 01:43:59 +08:00
|
|
|
|
|
|
|
if ((oct = ASN1_OCTET_STRING_new()) == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
inlen = ASN1_item_i2d(obj, &in, it);
|
|
|
|
if (!in) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR);
|
2015-01-22 11:40:55 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
if (!PKCS12_pbe_crypt_ex(algor, pass, passlen, in, inlen, &oct->data,
|
|
|
|
&oct->length, 1, ctx, propq)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR);
|
2015-01-22 11:40:55 +08:00
|
|
|
OPENSSL_free(in);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (zbuf)
|
|
|
|
OPENSSL_cleanse(in, inlen);
|
|
|
|
OPENSSL_free(in);
|
|
|
|
return oct;
|
|
|
|
err:
|
2015-04-30 23:30:03 +08:00
|
|
|
ASN1_OCTET_STRING_free(oct);
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
1999-03-29 07:17:34 +08:00
|
|
|
}
|
2021-02-17 15:56:36 +08:00
|
|
|
|
|
|
|
ASN1_OCTET_STRING *PKCS12_item_i2d_encrypt(X509_ALGOR *algor,
|
|
|
|
const ASN1_ITEM *it,
|
|
|
|
const char *pass, int passlen,
|
|
|
|
void *obj, int zbuf)
|
|
|
|
{
|
|
|
|
return PKCS12_item_i2d_encrypt_ex(algor, it, pass, passlen, obj, zbuf, NULL, NULL);
|
|
|
|
}
|