2019-09-05 11:53:20 +08:00
|
|
|
/*
|
2022-05-03 18:52:38 +08:00
|
|
|
* Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
|
2019-09-05 11:53:20 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2020-07-14 07:40:47 +08:00
|
|
|
/* We need to use some engine deprecated APIs */
|
|
|
|
#define OPENSSL_SUPPRESS_DEPRECATED
|
|
|
|
|
2019-09-05 11:53:20 +08:00
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/core_names.h>
|
2020-03-20 20:00:12 +08:00
|
|
|
#include <openssl/err.h>
|
2021-02-06 00:40:42 +08:00
|
|
|
#include <openssl/proverr.h>
|
2021-07-01 23:41:02 +08:00
|
|
|
#ifndef FIPS_MODULE
|
|
|
|
# include <openssl/engine.h>
|
2021-09-14 15:34:32 +08:00
|
|
|
# include "crypto/evp.h"
|
2021-07-01 23:41:02 +08:00
|
|
|
#endif
|
2019-10-04 21:25:59 +08:00
|
|
|
#include "prov/provider_util.h"
|
2019-09-05 11:53:20 +08:00
|
|
|
|
|
|
|
void ossl_prov_cipher_reset(PROV_CIPHER *pc)
|
|
|
|
{
|
|
|
|
EVP_CIPHER_free(pc->alloc_cipher);
|
|
|
|
pc->alloc_cipher = NULL;
|
|
|
|
pc->cipher = NULL;
|
2021-10-15 23:28:53 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
|
|
|
ENGINE_finish(pc->engine);
|
|
|
|
#endif
|
2019-09-05 11:53:20 +08:00
|
|
|
pc->engine = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
|
|
|
|
{
|
|
|
|
if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
|
|
|
|
return 0;
|
2021-10-15 23:28:53 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
|
|
|
if (src->engine != NULL && !ENGINE_init(src->engine)) {
|
|
|
|
EVP_CIPHER_free(src->alloc_cipher);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2019-09-05 11:53:20 +08:00
|
|
|
dst->engine = src->engine;
|
|
|
|
dst->cipher = src->cipher;
|
|
|
|
dst->alloc_cipher = src->alloc_cipher;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_common(const OSSL_PARAM params[], const char **propquery,
|
|
|
|
ENGINE **engine)
|
|
|
|
{
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
|
|
|
|
*propquery = NULL;
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
*propquery = p->data;
|
|
|
|
}
|
|
|
|
|
2021-10-15 23:28:53 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
|
|
|
ENGINE_finish(*engine);
|
|
|
|
#endif
|
2019-09-05 11:53:20 +08:00
|
|
|
*engine = NULL;
|
2019-09-11 17:17:00 +08:00
|
|
|
/* Inside the FIPS module, we don't support legacy ciphers */
|
2020-04-14 04:34:56 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
2020-08-11 23:17:00 +08:00
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
|
2019-09-05 11:53:20 +08:00
|
|
|
if (p != NULL) {
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
2021-10-15 23:28:53 +08:00
|
|
|
/* Get a structural reference */
|
2019-09-05 11:53:20 +08:00
|
|
|
*engine = ENGINE_by_id(p->data);
|
|
|
|
if (*engine == NULL)
|
|
|
|
return 0;
|
2021-10-15 23:28:53 +08:00
|
|
|
/* Get a functional reference */
|
|
|
|
if (!ENGINE_init(*engine)) {
|
|
|
|
ENGINE_free(*engine);
|
|
|
|
*engine = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Free the structural reference */
|
|
|
|
ENGINE_free(*engine);
|
2019-09-05 11:53:20 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
|
|
|
|
const OSSL_PARAM params[],
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *ctx)
|
2019-09-05 11:53:20 +08:00
|
|
|
{
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
const char *propquery;
|
|
|
|
|
2021-02-25 11:51:28 +08:00
|
|
|
if (params == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2019-09-05 11:53:20 +08:00
|
|
|
if (!load_common(params, &propquery, &pc->engine))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
|
|
|
|
if (p == NULL)
|
|
|
|
return 1;
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
EVP_CIPHER_free(pc->alloc_cipher);
|
2020-03-20 20:00:12 +08:00
|
|
|
ERR_set_mark();
|
2019-09-05 11:53:20 +08:00
|
|
|
pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
|
2021-09-14 15:34:32 +08:00
|
|
|
if (pc->cipher == NULL) {
|
|
|
|
const EVP_CIPHER *cipher;
|
|
|
|
|
|
|
|
cipher = EVP_get_cipherbyname(p->data);
|
|
|
|
/* Do not use global EVP_CIPHERs */
|
|
|
|
if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
|
|
|
|
pc->cipher = cipher;
|
|
|
|
}
|
2019-09-05 11:53:20 +08:00
|
|
|
#endif
|
2020-03-20 20:00:12 +08:00
|
|
|
if (pc->cipher != NULL)
|
|
|
|
ERR_pop_to_mark();
|
|
|
|
else
|
|
|
|
ERR_clear_last_mark();
|
2019-09-05 11:53:20 +08:00
|
|
|
return pc->cipher != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
|
|
|
|
{
|
|
|
|
return pc->cipher;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
|
|
|
|
{
|
|
|
|
return pc->engine;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ossl_prov_digest_reset(PROV_DIGEST *pd)
|
|
|
|
{
|
|
|
|
EVP_MD_free(pd->alloc_md);
|
|
|
|
pd->alloc_md = NULL;
|
|
|
|
pd->md = NULL;
|
2021-10-15 23:28:53 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
|
|
|
ENGINE_finish(pd->engine);
|
|
|
|
#endif
|
2019-09-05 11:53:20 +08:00
|
|
|
pd->engine = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
|
|
|
|
{
|
|
|
|
if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
|
|
|
|
return 0;
|
2021-10-15 23:28:53 +08:00
|
|
|
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
|
|
|
|
if (src->engine != NULL && !ENGINE_init(src->engine)) {
|
|
|
|
EVP_MD_free(src->alloc_md);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2019-09-05 11:53:20 +08:00
|
|
|
dst->engine = src->engine;
|
|
|
|
dst->md = src->md;
|
|
|
|
dst->alloc_md = src->alloc_md;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
|
2022-01-26 09:01:57 +08:00
|
|
|
const char *mdname, const char *propquery)
|
2020-09-18 16:55:16 +08:00
|
|
|
{
|
|
|
|
EVP_MD_free(pd->alloc_md);
|
|
|
|
pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
|
|
|
|
|
|
|
|
return pd->md;
|
|
|
|
}
|
|
|
|
|
2019-09-05 11:53:20 +08:00
|
|
|
int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
|
|
|
|
const OSSL_PARAM params[],
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *ctx)
|
2019-09-05 11:53:20 +08:00
|
|
|
{
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
const char *propquery;
|
|
|
|
|
2021-02-25 11:51:28 +08:00
|
|
|
if (params == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2019-09-05 11:53:20 +08:00
|
|
|
if (!load_common(params, &propquery, &pd->engine))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
|
|
|
|
if (p == NULL)
|
|
|
|
return 1;
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
|
2020-03-20 20:00:12 +08:00
|
|
|
ERR_set_mark();
|
2020-09-18 16:55:16 +08:00
|
|
|
ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
|
2020-04-14 04:34:56 +08:00
|
|
|
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
|
2021-09-14 15:34:32 +08:00
|
|
|
if (pd->md == NULL) {
|
|
|
|
const EVP_MD *md;
|
|
|
|
|
|
|
|
md = EVP_get_digestbyname(p->data);
|
|
|
|
/* Do not use global EVP_MDs */
|
|
|
|
if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
|
|
|
|
pd->md = md;
|
|
|
|
}
|
2019-09-05 11:53:20 +08:00
|
|
|
#endif
|
2020-03-20 20:00:12 +08:00
|
|
|
if (pd->md != NULL)
|
|
|
|
ERR_pop_to_mark();
|
|
|
|
else
|
|
|
|
ERR_clear_last_mark();
|
2019-09-05 11:53:20 +08:00
|
|
|
return pd->md != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
|
|
|
|
{
|
|
|
|
return pd->md;
|
|
|
|
}
|
|
|
|
|
|
|
|
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
|
|
|
|
{
|
|
|
|
return pd->engine;
|
|
|
|
}
|
|
|
|
|
2020-08-17 22:14:14 +08:00
|
|
|
int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
|
|
|
|
const OSSL_PARAM params[],
|
|
|
|
const char *ciphername,
|
|
|
|
const char *mdname,
|
|
|
|
const char *engine,
|
|
|
|
const char *properties,
|
|
|
|
const unsigned char *key,
|
|
|
|
size_t keylen)
|
|
|
|
{
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
OSSL_PARAM mac_params[6], *mp = mac_params;
|
|
|
|
|
|
|
|
if (params != NULL) {
|
|
|
|
if (mdname == NULL) {
|
|
|
|
if ((p = OSSL_PARAM_locate_const(params,
|
|
|
|
OSSL_ALG_PARAM_DIGEST)) != NULL) {
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
mdname = p->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ciphername == NULL) {
|
|
|
|
if ((p = OSSL_PARAM_locate_const(params,
|
|
|
|
OSSL_ALG_PARAM_CIPHER)) != NULL) {
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
ciphername = p->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (engine == NULL) {
|
|
|
|
if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
|
|
|
|
!= NULL) {
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
engine = p->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mdname != NULL)
|
|
|
|
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
|
|
|
|
(char *)mdname, 0);
|
|
|
|
if (ciphername != NULL)
|
|
|
|
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
|
|
|
|
(char *)ciphername, 0);
|
|
|
|
if (properties != NULL)
|
|
|
|
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
|
|
|
|
(char *)properties, 0);
|
|
|
|
|
|
|
|
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
|
|
|
|
if (engine != NULL)
|
|
|
|
*mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
|
|
|
|
(char *) engine, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (key != NULL)
|
|
|
|
*mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
|
|
|
|
(unsigned char *)key,
|
|
|
|
keylen);
|
|
|
|
|
|
|
|
*mp = OSSL_PARAM_construct_end();
|
|
|
|
|
|
|
|
return EVP_MAC_CTX_set_params(macctx, mac_params);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-09-19 17:47:46 +08:00
|
|
|
int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
|
|
|
|
const OSSL_PARAM params[],
|
|
|
|
const char *macname,
|
|
|
|
const char *ciphername,
|
|
|
|
const char *mdname,
|
2020-10-15 17:55:50 +08:00
|
|
|
OSSL_LIB_CTX *libctx)
|
2019-09-19 17:47:46 +08:00
|
|
|
{
|
|
|
|
const OSSL_PARAM *p;
|
|
|
|
const char *properties = NULL;
|
|
|
|
|
|
|
|
if (macname == NULL
|
|
|
|
&& (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
macname = p->data;
|
|
|
|
}
|
|
|
|
if ((p = OSSL_PARAM_locate_const(params,
|
|
|
|
OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
|
|
|
|
if (p->data_type != OSSL_PARAM_UTF8_STRING)
|
|
|
|
return 0;
|
|
|
|
properties = p->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we got a new mac name, we make a new EVP_MAC_CTX */
|
|
|
|
if (macname != NULL) {
|
|
|
|
EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
|
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
EVP_MAC_CTX_free(*macctx);
|
|
|
|
*macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
|
2019-09-19 17:47:46 +08:00
|
|
|
/* The context holds on to the MAC */
|
|
|
|
EVP_MAC_free(mac);
|
|
|
|
if (*macctx == NULL)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is no MAC yet (and therefore, no MAC context), we ignore
|
|
|
|
* all other parameters.
|
|
|
|
*/
|
|
|
|
if (*macctx == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2020-08-17 22:14:14 +08:00
|
|
|
if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
|
|
|
|
properties, NULL, 0))
|
2019-09-19 17:47:46 +08:00
|
|
|
return 1;
|
|
|
|
|
2020-06-18 16:26:22 +08:00
|
|
|
EVP_MAC_CTX_free(*macctx);
|
2019-09-19 17:47:46 +08:00
|
|
|
*macctx = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
2020-01-06 11:02:16 +08:00
|
|
|
|
|
|
|
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
|
|
|
|
OSSL_ALGORITHM *out)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if (out[0].algorithm_names == NULL) {
|
|
|
|
for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
|
|
|
|
if (in[i].capable == NULL || in[i].capable())
|
|
|
|
out[j++] = in[i].alg;
|
|
|
|
}
|
|
|
|
out[j++] = in[i].alg;
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 12:21:51 +08:00
|
|
|
|
|
|
|
/* Duplicate a lump of memory safely */
|
|
|
|
int ossl_prov_memdup(const void *src, size_t src_len,
|
|
|
|
unsigned char **dest, size_t *dest_len)
|
|
|
|
{
|
|
|
|
if (src != NULL) {
|
2022-09-29 19:57:34 +08:00
|
|
|
if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
|
2022-01-26 12:21:51 +08:00
|
|
|
return 0;
|
|
|
|
*dest_len = src_len;
|
|
|
|
} else {
|
|
|
|
*dest = NULL;
|
|
|
|
*dest_len = 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|