2016-05-17 15:38:09 -04:00
|
|
|
/*
|
2020-04-23 13:55:52 +01:00
|
|
|
* Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 10:52:47 +00:00
|
|
|
*
|
2018-12-06 13:40:06 +01:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 15:38:09 -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
|
1998-12-21 10:52:47 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-14 16:56:48 +02:00
|
|
|
#include "internal/cryptlib.h"
|
2003-03-20 23:34:28 +00:00
|
|
|
|
2020-03-05 12:50:31 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#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;
|
2020-01-11 00:04:56 +01:00
|
|
|
size_t keylen = 0;
|
|
|
|
int ret = 0;
|
|
|
|
EVP_PKEY_CTX *pctx = NULL;
|
2000-06-11 12:18:15 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
if (type) {
|
2015-12-13 22:08:41 +01:00
|
|
|
EVP_CIPHER_CTX_reset(ctx);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL))
|
2020-01-11 00:04:56 +01:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
2000-06-11 12:18:15 +00:00
|
|
|
|
2019-09-16 15:28:57 -04:00
|
|
|
if (priv == NULL)
|
2015-01-22 03:40:55 +00:00
|
|
|
return 1;
|
2000-06-11 12:18:15 +00:00
|
|
|
|
2020-01-11 00:04:56 +01:00
|
|
|
if ((pctx = EVP_PKEY_CTX_new(priv, NULL)) == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2020-01-11 00:04:56 +01:00
|
|
|
if (EVP_PKEY_decrypt_init(pctx) <= 0
|
|
|
|
|| EVP_PKEY_decrypt(pctx, NULL, &keylen, ek, ekl) <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2020-01-11 00:04:56 +01:00
|
|
|
if ((key = OPENSSL_malloc(keylen)) == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
}
|
2020-01-11 00:04:56 +01:00
|
|
|
|
|
|
|
if (EVP_PKEY_decrypt(pctx, key, &keylen, ek, ekl) <= 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!EVP_CIPHER_CTX_set_key_length(ctx, keylen)
|
|
|
|
|| !EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
1998-12-21 10:52:47 +00:00
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
ret = 1;
|
|
|
|
err:
|
2020-01-11 00:04:56 +01:00
|
|
|
EVP_PKEY_CTX_free(pctx);
|
|
|
|
OPENSSL_clear_free(key, keylen);
|
2017-10-17 23:04:09 +09: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 23:04:09 +09:00
|
|
|
return i;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|