• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 1995-2022 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 
EVP_OpenInit(EVP_CIPHER_CTX * ctx,const EVP_CIPHER * type,const unsigned char * ek,int ekl,const unsigned char * iv,EVP_PKEY * priv)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_MALLOC_FAILURE);
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         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
47         goto err;
48     }
49 
50     if (EVP_PKEY_decrypt(pctx, key, &keylen, ek, ekl) <= 0)
51         goto err;
52 
53     if (EVP_CIPHER_CTX_set_key_length(ctx, keylen) <= 0
54         || !EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
55         goto err;
56 
57     ret = 1;
58  err:
59     EVP_PKEY_CTX_free(pctx);
60     OPENSSL_clear_free(key, keylen);
61     return ret;
62 }
63 
EVP_OpenFinal(EVP_CIPHER_CTX * ctx,unsigned char * out,int * outl)64 int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
65 {
66     int i;
67 
68     i = EVP_DecryptFinal_ex(ctx, out, outl);
69     if (i)
70         i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
71     return i;
72 }
73