2016-05-17 19:38:09 +00:00
|
|
|
/*
|
2019-05-28 12:49:38 +00:00
|
|
|
* Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2016-05-17 19:38:09 +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
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2016-01-31 18:08:23 +00:00
|
|
|
#ifdef OPENSSL_NO_RSA
|
|
|
|
NON_EMPTY_TRANSLATION_UNIT
|
|
|
|
#else
|
2003-03-20 23:34:28 +00:00
|
|
|
|
2016-01-31 18:08:23 +00:00
|
|
|
# include <stdio.h>
|
2015-01-22 03:40:55 +00:00
|
|
|
# include <openssl/evp.h>
|
|
|
|
# include <openssl/objects.h>
|
|
|
|
# include <openssl/x509.h>
|
|
|
|
# include <openssl/rsa.h>
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2004-03-15 23:15:26 +00:00
|
|
|
int EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *ek, int ekl, const unsigned char *iv,
|
|
|
|
EVP_PKEY *priv)
|
|
|
|
{
|
|
|
|
unsigned char *key = NULL;
|
|
|
|
int i, size = 0, ret = 0;
|
2000-06-11 12:18:15 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (type) {
|
2015-12-13 21:08:41 +00:00
|
|
|
EVP_CIPHER_CTX_reset(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL))
|
|
|
|
return 0;
|
|
|
|
}
|
2000-06-11 12:18:15 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!priv)
|
|
|
|
return 1;
|
2000-06-11 12:18:15 +00:00
|
|
|
|
2016-01-19 00:21:12 +00:00
|
|
|
if (EVP_PKEY_id(priv) != EVP_PKEY_RSA) {
|
2015-01-22 03:40:55 +00:00
|
|
|
EVPerr(EVP_F_EVP_OPENINIT, EVP_R_PUBLIC_KEY_NOT_RSA);
|
|
|
|
goto err;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2016-01-19 00:21:12 +00:00
|
|
|
size = EVP_PKEY_size(priv);
|
2019-03-31 10:00:16 +00:00
|
|
|
key = OPENSSL_malloc(size);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (key == NULL) {
|
|
|
|
/* ERROR */
|
|
|
|
EVPerr(EVP_F_EVP_OPENINIT, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
i = EVP_PKEY_decrypt_old(key, ek, ekl, priv);
|
|
|
|
if ((i <= 0) || !EVP_CIPHER_CTX_set_key_length(ctx, i)) {
|
|
|
|
/* ERROR */
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
|
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = 1;
|
|
|
|
err:
|
2015-04-30 21:57:32 +00:00
|
|
|
OPENSSL_clear_free(key, size);
|
2017-10-17 14:04:09 +00:00
|
|
|
return ret;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
2015-01-22 03:40:55 +00:00
|
|
|
{
|
|
|
|
int i;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
i = EVP_DecryptFinal_ex(ctx, out, outl);
|
|
|
|
if (i)
|
|
|
|
i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
|
2017-10-17 14:04:09 +00:00
|
|
|
return i;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1999-04-27 01:14:46 +00:00
|
|
|
#endif
|