]>
Commit | Line | Data |
---|---|---|
aa6bb135 | 1 | /* |
33388b44 | 2 | * Copyright 1995-2020 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 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
4547a719 | 12 | #include "internal/provider.h" |
ec577822 | 13 | #include <openssl/rand.h> |
3c27208f | 14 | #include <openssl/rsa.h> |
ec577822 BM |
15 | #include <openssl/evp.h> |
16 | #include <openssl/objects.h> | |
17 | #include <openssl/x509.h> | |
d02b48c6 | 18 | |
0f113f3e MC |
19 | int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, |
20 | unsigned char **ek, int *ekl, unsigned char *iv, | |
21 | EVP_PKEY **pubk, int npubk) | |
22 | { | |
23 | unsigned char key[EVP_MAX_KEY_LENGTH]; | |
4547a719 P |
24 | const OSSL_PROVIDER *prov = EVP_CIPHER_provider(type); |
25 | OSSL_LIB_CTX *libctx = prov != NULL ? ossl_provider_libctx(prov) : NULL; | |
26 | EVP_PKEY_CTX *pctx = NULL; | |
3c184592 | 27 | int i, len; |
2eb2b4f3 | 28 | int rv = 0; |
0f113f3e MC |
29 | |
30 | if (type) { | |
846ec07d | 31 | EVP_CIPHER_CTX_reset(ctx); |
0f113f3e MC |
32 | if (!EVP_EncryptInit_ex(ctx, type, NULL, NULL, NULL)) |
33 | return 0; | |
34 | } | |
35 | if ((npubk <= 0) || !pubk) | |
36 | return 1; | |
9420b403 | 37 | |
0f113f3e MC |
38 | if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0) |
39 | return 0; | |
2eb2b4f3 | 40 | |
3c184592 | 41 | len = EVP_CIPHER_CTX_iv_length(ctx); |
4547a719 | 42 | if (len < 0 || RAND_priv_bytes_ex(libctx, iv, len) <= 0) |
3c184592 P |
43 | goto err; |
44 | ||
45 | len = EVP_CIPHER_CTX_key_length(ctx); | |
46 | if (len < 0) | |
2eb2b4f3 | 47 | goto err; |
d02b48c6 | 48 | |
0f113f3e | 49 | if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv)) |
2eb2b4f3 | 50 | goto err; |
58964a49 | 51 | |
0f113f3e | 52 | for (i = 0; i < npubk; i++) { |
3c184592 | 53 | size_t keylen = len; |
9420b403 | 54 | |
4547a719 P |
55 | pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pubk[i], NULL); |
56 | if (pctx == NULL) { | |
9420b403 | 57 | ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE); |
2eb2b4f3 SL |
58 | goto err; |
59 | } | |
9420b403 RL |
60 | |
61 | if (EVP_PKEY_encrypt_init(pctx) <= 0 | |
62 | || EVP_PKEY_encrypt(pctx, ek[i], &keylen, key, keylen) <= 0) | |
63 | goto err; | |
64 | ekl[i] = (int)keylen; | |
65 | EVP_PKEY_CTX_free(pctx); | |
0f113f3e | 66 | } |
4547a719 | 67 | pctx = NULL; |
2eb2b4f3 SL |
68 | rv = npubk; |
69 | err: | |
4547a719 | 70 | EVP_PKEY_CTX_free(pctx); |
2eb2b4f3 SL |
71 | OPENSSL_cleanse(key, sizeof(key)); |
72 | return rv; | |
0f113f3e | 73 | } |
d02b48c6 | 74 | |
e9ba6963 | 75 | int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) |
0f113f3e MC |
76 | { |
77 | int i; | |
78 | i = EVP_EncryptFinal_ex(ctx, out, outl); | |
79 | if (i) | |
80 | i = EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL); | |
81 | return i; | |
82 | } |