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;
32 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
34 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
36 dst->engine = src->engine;
37 dst->cipher = src->cipher;
38 dst->alloc_cipher = src->alloc_cipher;
42 static int load_common(const OSSL_PARAM params[], const char **propquery,
48 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
50 if (p->data_type != OSSL_PARAM_UTF8_STRING)
56 /* Inside the FIPS module, we don't support legacy ciphers */
57 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
58 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
60 if (p->data_type != OSSL_PARAM_UTF8_STRING)
62 ENGINE_finish(*engine);
63 *engine = ENGINE_by_id(p->data);
71 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
72 const OSSL_PARAM params[],
76 const char *propquery;
81 if (!load_common(params, &propquery, &pc->engine))
84 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
87 if (p->data_type != OSSL_PARAM_UTF8_STRING)
90 EVP_CIPHER_free(pc->alloc_cipher);
92 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
93 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
94 if (pc->cipher == NULL) {
95 const EVP_CIPHER *cipher;
97 cipher = EVP_get_cipherbyname(p->data);
98 /* Do not use global EVP_CIPHERs */
99 if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
103 if (pc->cipher != NULL)
106 ERR_clear_last_mark();
107 return pc->cipher != NULL;
110 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
115 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
120 void ossl_prov_digest_reset(PROV_DIGEST *pd)
122 EVP_MD_free(pd->alloc_md);
128 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
130 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
132 dst->engine = src->engine;
134 dst->alloc_md = src->alloc_md;
138 const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
139 const char *mdname, const char *propquery)
141 EVP_MD_free(pd->alloc_md);
142 pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
147 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
148 const OSSL_PARAM params[],
152 const char *propquery;
157 if (!load_common(params, &propquery, &pd->engine))
160 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
163 if (p->data_type != OSSL_PARAM_UTF8_STRING)
167 ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
168 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
169 if (pd->md == NULL) {
172 md = EVP_get_digestbyname(p->data);
173 /* Do not use global EVP_MDs */
174 if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
181 ERR_clear_last_mark();
182 return pd->md != NULL;
185 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
190 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
195 int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
196 const OSSL_PARAM params[],
197 const char *ciphername,
200 const char *properties,
201 const unsigned char *key,
205 OSSL_PARAM mac_params[6], *mp = mac_params;
207 if (params != NULL) {
208 if (mdname == NULL) {
209 if ((p = OSSL_PARAM_locate_const(params,
210 OSSL_ALG_PARAM_DIGEST)) != NULL) {
211 if (p->data_type != OSSL_PARAM_UTF8_STRING)
216 if (ciphername == NULL) {
217 if ((p = OSSL_PARAM_locate_const(params,
218 OSSL_ALG_PARAM_CIPHER)) != NULL) {
219 if (p->data_type != OSSL_PARAM_UTF8_STRING)
221 ciphername = p->data;
224 if (engine == NULL) {
225 if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
227 if (p->data_type != OSSL_PARAM_UTF8_STRING)
235 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
237 if (ciphername != NULL)
238 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
239 (char *)ciphername, 0);
240 if (properties != NULL)
241 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
242 (char *)properties, 0);
244 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
246 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
251 *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
252 (unsigned char *)key,
255 *mp = OSSL_PARAM_construct_end();
257 return EVP_MAC_CTX_set_params(macctx, mac_params);
261 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
262 const OSSL_PARAM params[],
264 const char *ciphername,
266 OSSL_LIB_CTX *libctx)
269 const char *properties = NULL;
272 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
273 if (p->data_type != OSSL_PARAM_UTF8_STRING)
277 if ((p = OSSL_PARAM_locate_const(params,
278 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
279 if (p->data_type != OSSL_PARAM_UTF8_STRING)
281 properties = p->data;
284 /* If we got a new mac name, we make a new EVP_MAC_CTX */
285 if (macname != NULL) {
286 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
288 EVP_MAC_CTX_free(*macctx);
289 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
290 /* The context holds on to the MAC */
297 * If there is no MAC yet (and therefore, no MAC context), we ignore
298 * all other parameters.
303 if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
304 properties, NULL, 0))
307 EVP_MAC_CTX_free(*macctx);
312 void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
317 if (out[0].algorithm_names == NULL) {
318 for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
319 if (in[i].capable == NULL || in[i].capable())
320 out[j++] = in[i].alg;
322 out[j++] = in[i].alg;