]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/p_open.c
Copyright consolidation 05/10
[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 *
aa6bb135
RS
4 * Licensed under the OpenSSL license (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
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;
26 int i, size = 0, ret = 0;
a91dedca 27
0f113f3e 28 if (type) {
846ec07d 29 EVP_CIPHER_CTX_reset(ctx);
0f113f3e
MC
30 if (!EVP_DecryptInit_ex(ctx, type, NULL, NULL, NULL))
31 return 0;
32 }
a91dedca 33
0f113f3e
MC
34 if (!priv)
35 return 1;
a91dedca 36
3aeb9348 37 if (EVP_PKEY_id(priv) != EVP_PKEY_RSA) {
0f113f3e
MC
38 EVPerr(EVP_F_EVP_OPENINIT, EVP_R_PUBLIC_KEY_NOT_RSA);
39 goto err;
40 }
d02b48c6 41
3aeb9348 42 size = EVP_PKEY_size(priv);
b196e7d9 43 key = OPENSSL_malloc(size + 2);
0f113f3e
MC
44 if (key == NULL) {
45 /* ERROR */
46 EVPerr(EVP_F_EVP_OPENINIT, ERR_R_MALLOC_FAILURE);
47 goto err;
48 }
d02b48c6 49
0f113f3e
MC
50 i = EVP_PKEY_decrypt_old(key, ek, ekl, priv);
51 if ((i <= 0) || !EVP_CIPHER_CTX_set_key_length(ctx, i)) {
52 /* ERROR */
53 goto err;
54 }
55 if (!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
56 goto err;
d02b48c6 57
0f113f3e
MC
58 ret = 1;
59 err:
4b45c6e5 60 OPENSSL_clear_free(key, size);
0f113f3e
MC
61 return (ret);
62}
d02b48c6 63
6b691a5c 64int EVP_OpenFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
0f113f3e
MC
65{
66 int i;
d02b48c6 67
0f113f3e
MC
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}
f5d7a031 73#endif