/src/openssl/crypto/evp/p_open.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
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 | 0 | { |
22 | 0 | unsigned char *key = NULL; |
23 | 0 | size_t keylen = 0; |
24 | 0 | int ret = 0; |
25 | 0 | EVP_PKEY_CTX *pctx = NULL; |
26 | |
|
27 | 0 | if (type) { |
28 | 0 | EVP_CIPHER_CTX_reset(ctx); |
29 | 0 | if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL)) |
30 | 0 | goto err; |
31 | 0 | } |
32 | | |
33 | 0 | if (priv == NULL) |
34 | 0 | return 1; |
35 | | |
36 | 0 | if ((pctx = EVP_PKEY_CTX_new(priv, NULL)) == NULL) { |
37 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
38 | 0 | goto err; |
39 | 0 | } |
40 | | |
41 | 0 | if (EVP_PKEY_decrypt_init(pctx) <= 0 |
42 | 0 | || EVP_PKEY_decrypt(pctx, NULL, &keylen, ek, ekl) <= 0) |
43 | 0 | goto err; |
44 | | |
45 | 0 | if ((key = OPENSSL_malloc(keylen)) == NULL) { |
46 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
47 | 0 | goto err; |
48 | 0 | } |
49 | | |
50 | 0 | if (EVP_PKEY_decrypt(pctx, key, &keylen, ek, ekl) <= 0) |
51 | 0 | goto err; |
52 | | |
53 | 0 | if (EVP_CIPHER_CTX_set_key_length(ctx, keylen) <= 0 |
54 | 0 | || !EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv)) |
55 | 0 | goto err; |
56 | | |
57 | 0 | ret = 1; |
58 | 0 | err: |
59 | 0 | EVP_PKEY_CTX_free(pctx); |
60 | 0 | OPENSSL_clear_free(key, keylen); |
61 | 0 | return ret; |
62 | 0 | } |
63 | | |
64 | | int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
65 | 0 | { |
66 | 0 | int i; |
67 | |
|
68 | 0 | i = EVP_DecryptFinal_ex(ctx, out, outl); |
69 | 0 | if (i) |
70 | 0 | i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL); |
71 | 0 | return i; |
72 | 0 | } |