| 1 |
/* |
| 2 |
* Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 5 |
* this file except in compliance with the License. You can obtain a copy |
| 6 |
* in the file LICENSE in the source distribution or at |
| 7 |
* https://www.openssl.org/source/license.html |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "internal/cryptlib.h" |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <openssl/evp.h> |
| 14 |
#include <openssl/objects.h> |
| 15 |
#include <openssl/x509.h> |
| 16 |
#include <openssl/rsa.h> |
| 17 |
|
| 18 |
int EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, |
| 19 |
const unsigned char *ek, int ekl, const unsigned char *iv, |
| 20 |
EVP_PKEY *priv) |
| 21 |
{ |
| 22 |
unsigned char *key = NULL; |
| 23 |
size_t keylen = 0; |
| 24 |
int ret = 0; |
| 25 |
EVP_PKEY_CTX *pctx = NULL; |
| 26 |
|
| 27 |
if (type) { |
| 28 |
EVP_CIPHER_CTX_reset(ctx); |
| 29 |
if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL)) |
| 30 |
goto err; |
| 31 |
} |
| 32 |
|
| 33 |
if (priv == NULL) |
| 34 |
return 1; |
| 35 |
|
| 36 |
if ((pctx = EVP_PKEY_CTX_new(priv, NULL)) == NULL) { |
| 37 |
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
| 38 |
goto err; |
| 39 |
} |
| 40 |
|
| 41 |
if (EVP_PKEY_decrypt_init(pctx) <= 0 |
| 42 |
|| EVP_PKEY_decrypt(pctx, NULL, &keylen, ek, ekl) <= 0) |
| 43 |
goto err; |
| 44 |
|
| 45 |
if ((key = OPENSSL_malloc(keylen)) == NULL) |
| 46 |
goto err; |
| 47 |
|
| 48 |
if (EVP_PKEY_decrypt(pctx, key, &keylen, ek, ekl) <= 0) |
| 49 |
goto err; |
| 50 |
|
| 51 |
if (EVP_CIPHER_CTX_set_key_length(ctx, (int)keylen) <= 0 |
| 52 |
|| !EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv)) |
| 53 |
goto err; |
| 54 |
|
| 55 |
ret = 1; |
| 56 |
err: |
| 57 |
EVP_PKEY_CTX_free(pctx); |
| 58 |
OPENSSL_clear_free(key, keylen); |
| 59 |
return ret; |
| 60 |
} |
| 61 |
|
| 62 |
int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
| 63 |
{ |
| 64 |
int i; |
| 65 |
|
| 66 |
i = EVP_DecryptFinal_ex(ctx, out, outl); |
| 67 |
if (i) |
| 68 |
i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL); |
| 69 |
return i; |
| 70 |
} |