2016-05-18 03:38:09 +08:00
|
|
|
/*
|
2021-05-20 21:22:33 +08:00
|
|
|
* Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
|
1998-12-21 18:52:47 +08:00
|
|
|
*
|
2018-12-06 20:40:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 03:38:09 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2021-05-17 10:18:53 +08:00
|
|
|
#include "internal/provider.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/rand.h>
|
2016-03-19 02:30:20 +08:00
|
|
|
#include <openssl/rsa.h>
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/objects.h>
|
|
|
|
#include <openssl/x509.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
|
|
|
|
unsigned char **ek, int *ekl, unsigned char *iv,
|
|
|
|
EVP_PKEY **pubk, int npubk)
|
|
|
|
{
|
|
|
|
unsigned char key[EVP_MAX_KEY_LENGTH];
|
2021-05-24 06:59:36 +08:00
|
|
|
const OSSL_PROVIDER *prov;
|
|
|
|
OSSL_LIB_CTX *libctx = NULL;
|
2021-05-17 10:18:53 +08:00
|
|
|
EVP_PKEY_CTX *pctx = NULL;
|
2021-05-24 06:59:36 +08:00
|
|
|
const EVP_CIPHER *cipher;
|
2021-05-17 07:38:29 +08:00
|
|
|
int i, len;
|
2018-09-06 06:34:45 +08:00
|
|
|
int rv = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2021-05-24 06:59:36 +08:00
|
|
|
if (type != NULL) {
|
2015-12-14 05:08:41 +08:00
|
|
|
EVP_CIPHER_CTX_reset(ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!EVP_EncryptInit_ex(ctx, type, NULL, NULL, NULL))
|
|
|
|
return 0;
|
|
|
|
}
|
2021-05-24 06:59:36 +08:00
|
|
|
if ((cipher = EVP_CIPHER_CTX_get0_cipher(ctx)) != NULL
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
&& (prov = EVP_CIPHER_get0_provider(cipher)) != NULL)
|
2021-05-24 06:59:36 +08:00
|
|
|
libctx = ossl_provider_libctx(prov);
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((npubk <= 0) || !pubk)
|
|
|
|
return 1;
|
2020-01-11 07:04:56 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (EVP_CIPHER_CTX_rand_key(ctx, key) <= 0)
|
|
|
|
return 0;
|
2018-09-06 06:34:45 +08:00
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
len = EVP_CIPHER_CTX_get_iv_length(ctx);
|
2021-05-28 12:46:40 +08:00
|
|
|
if (len < 0 || RAND_priv_bytes_ex(libctx, iv, len, 0) <= 0)
|
2021-05-17 07:38:29 +08:00
|
|
|
goto err;
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
len = EVP_CIPHER_CTX_get_key_length(ctx);
|
2021-05-17 07:38:29 +08:00
|
|
|
if (len < 0)
|
2018-09-06 06:34:45 +08:00
|
|
|
goto err;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv))
|
2018-09-06 06:34:45 +08:00
|
|
|
goto err;
|
1998-12-21 18:56:39 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
for (i = 0; i < npubk; i++) {
|
2021-05-17 07:38:29 +08:00
|
|
|
size_t keylen = len;
|
2020-01-11 07:04:56 +08:00
|
|
|
|
2021-05-17 10:18:53 +08:00
|
|
|
pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pubk[i], NULL);
|
|
|
|
if (pctx == NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
|
2018-09-06 06:34:45 +08:00
|
|
|
goto err;
|
|
|
|
}
|
2020-01-11 07:04:56 +08:00
|
|
|
|
|
|
|
if (EVP_PKEY_encrypt_init(pctx) <= 0
|
|
|
|
|| EVP_PKEY_encrypt(pctx, ek[i], &keylen, key, keylen) <= 0)
|
|
|
|
goto err;
|
|
|
|
ekl[i] = (int)keylen;
|
|
|
|
EVP_PKEY_CTX_free(pctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2021-05-17 10:18:53 +08:00
|
|
|
pctx = NULL;
|
2018-09-06 06:34:45 +08:00
|
|
|
rv = npubk;
|
|
|
|
err:
|
2021-05-17 10:18:53 +08:00
|
|
|
EVP_PKEY_CTX_free(pctx);
|
2018-09-06 06:34:45 +08:00
|
|
|
OPENSSL_cleanse(key, sizeof(key));
|
|
|
|
return rv;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2002-05-12 01:37:08 +08:00
|
|
|
int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
i = EVP_EncryptFinal_ex(ctx, out, outl);
|
|
|
|
if (i)
|
|
|
|
i = EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, NULL);
|
|
|
|
return i;
|
|
|
|
}
|