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