2 * Copyright 2019-2020 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 "prov/provider_util.h"
17 #include "prov/providercommonerr.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 /* TODO legacy stuff, to be removed */
53 /* Inside the FIPS module, we don't support legacy ciphers */
54 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
55 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
57 if (p->data_type != OSSL_PARAM_UTF8_STRING)
59 ENGINE_finish(*engine);
60 *engine = ENGINE_by_id(p->data);
68 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
69 const OSSL_PARAM params[],
73 const char *propquery;
75 if (!load_common(params, &propquery, &pc->engine))
78 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
81 if (p->data_type != OSSL_PARAM_UTF8_STRING)
84 EVP_CIPHER_free(pc->alloc_cipher);
86 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
87 /* TODO legacy stuff, to be removed */
88 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
89 if (pc->cipher == NULL)
90 pc->cipher = EVP_get_cipherbyname(p->data);
92 if (pc->cipher != NULL)
95 ERR_clear_last_mark();
96 return pc->cipher != NULL;
99 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
104 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
109 void ossl_prov_digest_reset(PROV_DIGEST *pd)
111 EVP_MD_free(pd->alloc_md);
117 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
119 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
121 dst->engine = src->engine;
123 dst->alloc_md = src->alloc_md;
127 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
128 const OSSL_PARAM params[],
132 const char *propquery;
134 if (!load_common(params, &propquery, &pd->engine))
138 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
141 if (p->data_type != OSSL_PARAM_UTF8_STRING)
144 EVP_MD_free(pd->alloc_md);
146 pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
147 /* TODO legacy stuff, to be removed */
148 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
150 pd->md = EVP_get_digestbyname(p->data);
155 ERR_clear_last_mark();
156 return pd->md != NULL;
159 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
164 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
169 int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
170 const OSSL_PARAM params[],
171 const char *ciphername,
174 const char *properties,
175 const unsigned char *key,
179 OSSL_PARAM mac_params[6], *mp = mac_params;
181 if (params != NULL) {
182 if (mdname == NULL) {
183 if ((p = OSSL_PARAM_locate_const(params,
184 OSSL_ALG_PARAM_DIGEST)) != NULL) {
185 if (p->data_type != OSSL_PARAM_UTF8_STRING)
190 if (ciphername == NULL) {
191 if ((p = OSSL_PARAM_locate_const(params,
192 OSSL_ALG_PARAM_CIPHER)) != NULL) {
193 if (p->data_type != OSSL_PARAM_UTF8_STRING)
195 ciphername = p->data;
198 if (engine == NULL) {
199 if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
201 if (p->data_type != OSSL_PARAM_UTF8_STRING)
209 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
211 if (ciphername != NULL)
212 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
213 (char *)ciphername, 0);
214 if (properties != NULL)
215 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
216 (char *)properties, 0);
218 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
220 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
225 *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
226 (unsigned char *)key,
229 *mp = OSSL_PARAM_construct_end();
231 return EVP_MAC_CTX_set_params(macctx, mac_params);
235 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
236 const OSSL_PARAM params[],
238 const char *ciphername,
243 const char *properties = NULL;
246 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
247 if (p->data_type != OSSL_PARAM_UTF8_STRING)
251 if ((p = OSSL_PARAM_locate_const(params,
252 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
253 if (p->data_type != OSSL_PARAM_UTF8_STRING)
255 properties = p->data;
258 /* If we got a new mac name, we make a new EVP_MAC_CTX */
259 if (macname != NULL) {
260 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
262 EVP_MAC_CTX_free(*macctx);
263 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
264 /* The context holds on to the MAC */
271 * If there is no MAC yet (and therefore, no MAC context), we ignore
272 * all other parameters.
277 if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
278 properties, NULL, 0))
281 EVP_MAC_CTX_free(*macctx);
286 void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
291 if (out[0].algorithm_names == NULL) {
292 for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
293 if (in[i].capable == NULL || in[i].capable())
294 out[j++] = in[i].alg;
296 out[j++] = in[i].alg;