]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p_open.c
Params: add argument to the _from_text calls to indicate if the param exists.
[thirdparty/openssl.git] / crypto / evp / p_open.c
CommitLineData
aa6bb135
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
4a8b0c55 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
d02b48c6
RE
8 */
9
b39fc560 10#include "internal/cryptlib.h"
effaf4de
RS
11#ifdef OPENSSL_NO_RSA
12NON_EMPTY_TRANSLATION_UNIT
13#else
be9bec9b 14
effaf4de 15# include <stdio.h>
0f113f3e
MC
16# include <openssl/evp.h>
17# include <openssl/objects.h>
18# include <openssl/x509.h>
19# include <openssl/rsa.h>
d02b48c6 20
875a644a 21int EVP_OpenInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
0f113f3e
MC
22 const unsigned char *ek, int ekl, const unsigned char *iv,
23 EVP_PKEY *priv)
24{
25 unsigned char *key = NULL;
9420b403
RL
26 size_t keylen = 0;
27 int ret = 0;
28 EVP_PKEY_CTX *pctx = NULL;
a91dedca 29
0f113f3e 30 if (type) {
846ec07d 31 EVP_CIPHER_CTX_reset(ctx);
0f113f3e 32 if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL))
9420b403 33 goto err;
0f113f3e 34 }
a91dedca 35
12a765a5 36 if (priv == NULL)
0f113f3e 37 return 1;
a91dedca 38
9420b403
RL
39 if ((pctx = EVP_PKEY_CTX_new(priv, NULL)) == NULL) {
40 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
41 goto err;
42 }
d02b48c6 43
9420b403
RL
44 if (EVP_PKEY_decrypt_init(pctx) <= 0
45 || EVP_PKEY_decrypt(pctx, NULL, &keylen, ek, ekl) <= 0)
0f113f3e 46 goto err;
d02b48c6 47
9420b403
RL
48 if ((key = OPENSSL_malloc(keylen)) == NULL) {
49 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
50 goto err;
51 }
9420b403
RL
52
53 if (EVP_PKEY_decrypt(pctx, key, &keylen, ek, ekl) <= 0)
54 goto err;
55
56 if (!EVP_CIPHER_CTX_set_key_length(ctx, keylen)
57 || !EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
0f113f3e 58 goto err;
d02b48c6 59
0f113f3e
MC
60 ret = 1;
61 err:
9420b403
RL
62 EVP_PKEY_CTX_free(pctx);
63 OPENSSL_clear_free(key, keylen);
26a7d938 64 return ret;
0f113f3e 65}
d02b48c6 66
6b691a5c 67int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
68{
69 int i;
d02b48c6 70
0f113f3e
MC
71 i = EVP_DecryptFinal_ex(ctx, out, outl);
72 if (i)
73 i = EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
26a7d938 74 return i;
0f113f3e 75}
f5d7a031 76#endif