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>
18 # include <openssl/engine.h>
19 # include "crypto/evp.h"
21 #include "prov/provider_util.h"
22 #include "internal/nelem.h"
24 void ossl_prov_cipher_reset(PROV_CIPHER *pc)
26 EVP_CIPHER_free(pc->alloc_cipher);
27 pc->alloc_cipher = NULL;
29 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30 ENGINE_finish(pc->engine);
35 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
37 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
39 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
40 if (src->engine != NULL && !ENGINE_init(src->engine)) {
41 EVP_CIPHER_free(src->alloc_cipher);
45 dst->engine = src->engine;
46 dst->cipher = src->cipher;
47 dst->alloc_cipher = src->alloc_cipher;
51 static int load_common(const OSSL_PARAM params[], const char **propquery,
57 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
59 if (p->data_type != OSSL_PARAM_UTF8_STRING)
64 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65 ENGINE_finish(*engine);
68 /* Inside the FIPS module, we don't support legacy ciphers */
69 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
72 if (p->data_type != OSSL_PARAM_UTF8_STRING)
74 /* Get a structural reference */
75 *engine = ENGINE_by_id(p->data);
78 /* Get a functional reference */
79 if (!ENGINE_init(*engine)) {
84 /* Free the structural reference */
91 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
92 const OSSL_PARAM params[],
96 const char *propquery;
101 if (!load_common(params, &propquery, &pc->engine))
104 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
107 if (p->data_type != OSSL_PARAM_UTF8_STRING)
110 EVP_CIPHER_free(pc->alloc_cipher);
112 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
113 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
114 if (pc->cipher == NULL) {
115 const EVP_CIPHER *cipher;
117 cipher = EVP_get_cipherbyname(p->data);
118 /* Do not use global EVP_CIPHERs */
119 if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
123 if (pc->cipher != NULL)
126 ERR_clear_last_mark();
127 return pc->cipher != NULL;
130 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
135 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
140 void ossl_prov_digest_reset(PROV_DIGEST *pd)
142 EVP_MD_free(pd->alloc_md);
145 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146 ENGINE_finish(pd->engine);
151 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
153 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
155 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156 if (src->engine != NULL && !ENGINE_init(src->engine)) {
157 EVP_MD_free(src->alloc_md);
161 dst->engine = src->engine;
163 dst->alloc_md = src->alloc_md;
167 const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
168 const char *mdname, const char *propquery)
170 EVP_MD_free(pd->alloc_md);
171 pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
176 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177 const OSSL_PARAM params[],
181 const char *propquery;
186 if (!load_common(params, &propquery, &pd->engine))
189 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
192 if (p->data_type != OSSL_PARAM_UTF8_STRING)
196 ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198 if (pd->md == NULL) {
201 md = EVP_get_digestbyname(p->data);
202 /* Do not use global EVP_MDs */
203 if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
210 ERR_clear_last_mark();
211 return pd->md != NULL;
214 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
219 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
224 int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
225 const OSSL_PARAM params[],
226 const char *ciphername,
229 const char *properties,
230 const unsigned char *key,
234 OSSL_PARAM mac_params[6], *mp = mac_params;
236 if (params != NULL) {
237 if (mdname == NULL) {
238 if ((p = OSSL_PARAM_locate_const(params,
239 OSSL_ALG_PARAM_DIGEST)) != NULL) {
240 if (p->data_type != OSSL_PARAM_UTF8_STRING)
245 if (ciphername == NULL) {
246 if ((p = OSSL_PARAM_locate_const(params,
247 OSSL_ALG_PARAM_CIPHER)) != NULL) {
248 if (p->data_type != OSSL_PARAM_UTF8_STRING)
250 ciphername = p->data;
253 if (engine == NULL) {
254 if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
256 if (p->data_type != OSSL_PARAM_UTF8_STRING)
264 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
266 if (ciphername != NULL)
267 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
268 (char *)ciphername, 0);
269 if (properties != NULL)
270 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
271 (char *)properties, 0);
273 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
275 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
280 *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
281 (unsigned char *)key,
284 *mp = OSSL_PARAM_construct_end();
286 return EVP_MAC_CTX_set_params(macctx, mac_params);
290 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
291 const OSSL_PARAM params[],
293 const char *ciphername,
295 OSSL_LIB_CTX *libctx)
298 const char *properties = NULL;
301 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
302 if (p->data_type != OSSL_PARAM_UTF8_STRING)
306 if ((p = OSSL_PARAM_locate_const(params,
307 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
308 if (p->data_type != OSSL_PARAM_UTF8_STRING)
310 properties = p->data;
313 /* If we got a new mac name, we make a new EVP_MAC_CTX */
314 if (macname != NULL) {
315 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
317 EVP_MAC_CTX_free(*macctx);
318 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
319 /* The context holds on to the MAC */
326 * If there is no MAC yet (and therefore, no MAC context), we ignore
327 * all other parameters.
332 if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
333 properties, NULL, 0))
336 EVP_MAC_CTX_free(*macctx);
341 void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
346 if (out[0].algorithm_names == NULL) {
347 for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
348 if (in[i].capable == NULL || in[i].capable())
349 out[j++] = in[i].alg;
351 out[j++] = in[i].alg;