2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/evp.h>
14 #include <openssl/core_names.h>
15 #include <openssl/err.h>
16 #include <openssl/proverr.h>
17 #include "prov/provider_util.h"
18 #include "internal/nelem.h"
20 void ossl_prov_cipher_reset(PROV_CIPHER *pc)
22 EVP_CIPHER_free(pc->alloc_cipher);
23 pc->alloc_cipher = NULL;
28 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
30 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
32 dst->engine = src->engine;
33 dst->cipher = src->cipher;
34 dst->alloc_cipher = src->alloc_cipher;
38 static int load_common(const OSSL_PARAM params[], const char **propquery,
44 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
46 if (p->data_type != OSSL_PARAM_UTF8_STRING)
52 /* Inside the FIPS module, we don't support legacy ciphers */
53 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
54 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
56 if (p->data_type != OSSL_PARAM_UTF8_STRING)
58 ENGINE_finish(*engine);
59 *engine = ENGINE_by_id(p->data);
67 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
68 const OSSL_PARAM params[],
72 const char *propquery;
77 if (!load_common(params, &propquery, &pc->engine))
80 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
83 if (p->data_type != OSSL_PARAM_UTF8_STRING)
86 EVP_CIPHER_free(pc->alloc_cipher);
88 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
89 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
90 if (pc->cipher == NULL)
91 pc->cipher = EVP_get_cipherbyname(p->data);
93 if (pc->cipher != NULL)
96 ERR_clear_last_mark();
97 return pc->cipher != NULL;
100 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
105 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
110 void ossl_prov_digest_reset(PROV_DIGEST *pd)
112 EVP_MD_free(pd->alloc_md);
118 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
120 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
122 dst->engine = src->engine;
124 dst->alloc_md = src->alloc_md;
128 const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
129 const char *mdname, const char *propquery)
131 EVP_MD_free(pd->alloc_md);
132 pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
137 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
138 const OSSL_PARAM params[],
142 const char *propquery;
147 if (!load_common(params, &propquery, &pd->engine))
150 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
153 if (p->data_type != OSSL_PARAM_UTF8_STRING)
157 ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
158 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
160 pd->md = EVP_get_digestbyname(p->data);
165 ERR_clear_last_mark();
166 return pd->md != NULL;
169 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
174 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
179 int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
180 const OSSL_PARAM params[],
181 const char *ciphername,
184 const char *properties,
185 const unsigned char *key,
189 OSSL_PARAM mac_params[6], *mp = mac_params;
191 if (params != NULL) {
192 if (mdname == NULL) {
193 if ((p = OSSL_PARAM_locate_const(params,
194 OSSL_ALG_PARAM_DIGEST)) != NULL) {
195 if (p->data_type != OSSL_PARAM_UTF8_STRING)
200 if (ciphername == NULL) {
201 if ((p = OSSL_PARAM_locate_const(params,
202 OSSL_ALG_PARAM_CIPHER)) != NULL) {
203 if (p->data_type != OSSL_PARAM_UTF8_STRING)
205 ciphername = p->data;
208 if (engine == NULL) {
209 if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
211 if (p->data_type != OSSL_PARAM_UTF8_STRING)
219 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
221 if (ciphername != NULL)
222 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
223 (char *)ciphername, 0);
224 if (properties != NULL)
225 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
226 (char *)properties, 0);
228 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
230 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
235 *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
236 (unsigned char *)key,
239 *mp = OSSL_PARAM_construct_end();
241 return EVP_MAC_CTX_set_params(macctx, mac_params);
245 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
246 const OSSL_PARAM params[],
248 const char *ciphername,
250 OSSL_LIB_CTX *libctx)
253 const char *properties = NULL;
256 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
257 if (p->data_type != OSSL_PARAM_UTF8_STRING)
261 if ((p = OSSL_PARAM_locate_const(params,
262 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
263 if (p->data_type != OSSL_PARAM_UTF8_STRING)
265 properties = p->data;
268 /* If we got a new mac name, we make a new EVP_MAC_CTX */
269 if (macname != NULL) {
270 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
272 EVP_MAC_CTX_free(*macctx);
273 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
274 /* The context holds on to the MAC */
281 * If there is no MAC yet (and therefore, no MAC context), we ignore
282 * all other parameters.
287 if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
288 properties, NULL, 0))
291 EVP_MAC_CTX_free(*macctx);
296 void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
301 if (out[0].algorithm_names == NULL) {
302 for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
303 if (in[i].capable == NULL || in[i].capable())
304 out[j++] = in[i].alg;
306 out[j++] = in[i].alg;