xref: /freebsd/crypto/openssl/providers/common/provider_util.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1b077aed3SPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2019-2025 The OpenSSL Project Authors. All Rights Reserved.
3b077aed3SPierre Pronchery  *
4b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8b077aed3SPierre Pronchery  */
9b077aed3SPierre Pronchery 
10b077aed3SPierre Pronchery /* We need to use some engine deprecated APIs */
11b077aed3SPierre Pronchery #define OPENSSL_SUPPRESS_DEPRECATED
12b077aed3SPierre Pronchery 
13b077aed3SPierre Pronchery #include <openssl/evp.h>
14b077aed3SPierre Pronchery #include <openssl/core_names.h>
15b077aed3SPierre Pronchery #include <openssl/err.h>
16b077aed3SPierre Pronchery #include <openssl/proverr.h>
17b077aed3SPierre Pronchery #ifndef FIPS_MODULE
18b077aed3SPierre Pronchery # include <openssl/engine.h>
19b077aed3SPierre Pronchery # include "crypto/evp.h"
20b077aed3SPierre Pronchery #endif
21*e7be843bSPierre Pronchery #include "prov/providercommon.h"
22b077aed3SPierre Pronchery #include "prov/provider_util.h"
23b077aed3SPierre Pronchery 
ossl_prov_cipher_reset(PROV_CIPHER * pc)24b077aed3SPierre Pronchery void ossl_prov_cipher_reset(PROV_CIPHER *pc)
25b077aed3SPierre Pronchery {
26b077aed3SPierre Pronchery     EVP_CIPHER_free(pc->alloc_cipher);
27b077aed3SPierre Pronchery     pc->alloc_cipher = NULL;
28b077aed3SPierre Pronchery     pc->cipher = NULL;
29b077aed3SPierre Pronchery #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30b077aed3SPierre Pronchery     ENGINE_finish(pc->engine);
31b077aed3SPierre Pronchery #endif
32b077aed3SPierre Pronchery     pc->engine = NULL;
33b077aed3SPierre Pronchery }
34b077aed3SPierre Pronchery 
ossl_prov_cipher_copy(PROV_CIPHER * dst,const PROV_CIPHER * src)35b077aed3SPierre Pronchery int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
36b077aed3SPierre Pronchery {
37b077aed3SPierre Pronchery     if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
38b077aed3SPierre Pronchery         return 0;
39b077aed3SPierre Pronchery #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
40b077aed3SPierre Pronchery     if (src->engine != NULL && !ENGINE_init(src->engine)) {
41b077aed3SPierre Pronchery         EVP_CIPHER_free(src->alloc_cipher);
42b077aed3SPierre Pronchery         return 0;
43b077aed3SPierre Pronchery     }
44b077aed3SPierre Pronchery #endif
45b077aed3SPierre Pronchery     dst->engine = src->engine;
46b077aed3SPierre Pronchery     dst->cipher = src->cipher;
47b077aed3SPierre Pronchery     dst->alloc_cipher = src->alloc_cipher;
48b077aed3SPierre Pronchery     return 1;
49b077aed3SPierre Pronchery }
50b077aed3SPierre Pronchery 
load_common(const OSSL_PARAM params[],const char ** propquery,ENGINE ** engine)51b077aed3SPierre Pronchery static int load_common(const OSSL_PARAM params[], const char **propquery,
52b077aed3SPierre Pronchery                        ENGINE **engine)
53b077aed3SPierre Pronchery {
54b077aed3SPierre Pronchery     const OSSL_PARAM *p;
55b077aed3SPierre Pronchery 
56b077aed3SPierre Pronchery     *propquery = NULL;
57b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
58b077aed3SPierre Pronchery     if (p != NULL) {
59b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_UTF8_STRING)
60b077aed3SPierre Pronchery             return 0;
61b077aed3SPierre Pronchery         *propquery = p->data;
62b077aed3SPierre Pronchery     }
63b077aed3SPierre Pronchery 
64b077aed3SPierre Pronchery #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65b077aed3SPierre Pronchery     ENGINE_finish(*engine);
66b077aed3SPierre Pronchery #endif
67b077aed3SPierre Pronchery     *engine = NULL;
68b077aed3SPierre Pronchery     /* Inside the FIPS module, we don't support legacy ciphers */
69b077aed3SPierre Pronchery #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
71b077aed3SPierre Pronchery     if (p != NULL) {
72b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_UTF8_STRING)
73b077aed3SPierre Pronchery             return 0;
74b077aed3SPierre Pronchery         /* Get a structural reference */
75b077aed3SPierre Pronchery         *engine = ENGINE_by_id(p->data);
76b077aed3SPierre Pronchery         if (*engine == NULL)
77b077aed3SPierre Pronchery             return 0;
78b077aed3SPierre Pronchery         /* Get a functional reference */
79b077aed3SPierre Pronchery         if (!ENGINE_init(*engine)) {
80b077aed3SPierre Pronchery             ENGINE_free(*engine);
81b077aed3SPierre Pronchery             *engine = NULL;
82b077aed3SPierre Pronchery             return 0;
83b077aed3SPierre Pronchery         }
84b077aed3SPierre Pronchery         /* Free the structural reference */
85b077aed3SPierre Pronchery         ENGINE_free(*engine);
86b077aed3SPierre Pronchery     }
87b077aed3SPierre Pronchery #endif
88b077aed3SPierre Pronchery     return 1;
89b077aed3SPierre Pronchery }
90b077aed3SPierre Pronchery 
ossl_prov_cipher_load_from_params(PROV_CIPHER * pc,const OSSL_PARAM params[],OSSL_LIB_CTX * ctx)91b077aed3SPierre Pronchery int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
92b077aed3SPierre Pronchery                                       const OSSL_PARAM params[],
93b077aed3SPierre Pronchery                                       OSSL_LIB_CTX *ctx)
94b077aed3SPierre Pronchery {
95b077aed3SPierre Pronchery     const OSSL_PARAM *p;
96b077aed3SPierre Pronchery     const char *propquery;
97b077aed3SPierre Pronchery 
98*e7be843bSPierre Pronchery     if (ossl_param_is_empty(params))
99b077aed3SPierre Pronchery         return 1;
100b077aed3SPierre Pronchery 
101b077aed3SPierre Pronchery     if (!load_common(params, &propquery, &pc->engine))
102b077aed3SPierre Pronchery         return 0;
103b077aed3SPierre Pronchery 
104b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
105b077aed3SPierre Pronchery     if (p == NULL)
106b077aed3SPierre Pronchery         return 1;
107b077aed3SPierre Pronchery     if (p->data_type != OSSL_PARAM_UTF8_STRING)
108b077aed3SPierre Pronchery         return 0;
109b077aed3SPierre Pronchery 
110b077aed3SPierre Pronchery     EVP_CIPHER_free(pc->alloc_cipher);
111b077aed3SPierre Pronchery     ERR_set_mark();
112b077aed3SPierre Pronchery     pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
113b077aed3SPierre Pronchery #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
114b077aed3SPierre Pronchery     if (pc->cipher == NULL) {
115b077aed3SPierre Pronchery         const EVP_CIPHER *cipher;
116b077aed3SPierre Pronchery 
117b077aed3SPierre Pronchery         cipher = EVP_get_cipherbyname(p->data);
118b077aed3SPierre Pronchery         /* Do not use global EVP_CIPHERs */
119b077aed3SPierre Pronchery         if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
120b077aed3SPierre Pronchery             pc->cipher = cipher;
121b077aed3SPierre Pronchery     }
122b077aed3SPierre Pronchery #endif
123b077aed3SPierre Pronchery     if (pc->cipher != NULL)
124b077aed3SPierre Pronchery         ERR_pop_to_mark();
125b077aed3SPierre Pronchery     else
126b077aed3SPierre Pronchery         ERR_clear_last_mark();
127b077aed3SPierre Pronchery     return pc->cipher != NULL;
128b077aed3SPierre Pronchery }
129b077aed3SPierre Pronchery 
ossl_prov_cipher_cipher(const PROV_CIPHER * pc)130b077aed3SPierre Pronchery const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
131b077aed3SPierre Pronchery {
132b077aed3SPierre Pronchery     return pc->cipher;
133b077aed3SPierre Pronchery }
134b077aed3SPierre Pronchery 
ossl_prov_cipher_engine(const PROV_CIPHER * pc)135b077aed3SPierre Pronchery ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
136b077aed3SPierre Pronchery {
137b077aed3SPierre Pronchery     return pc->engine;
138b077aed3SPierre Pronchery }
139b077aed3SPierre Pronchery 
ossl_prov_digest_reset(PROV_DIGEST * pd)140b077aed3SPierre Pronchery void ossl_prov_digest_reset(PROV_DIGEST *pd)
141b077aed3SPierre Pronchery {
142b077aed3SPierre Pronchery     EVP_MD_free(pd->alloc_md);
143b077aed3SPierre Pronchery     pd->alloc_md = NULL;
144b077aed3SPierre Pronchery     pd->md = NULL;
145b077aed3SPierre Pronchery #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146b077aed3SPierre Pronchery     ENGINE_finish(pd->engine);
147b077aed3SPierre Pronchery #endif
148b077aed3SPierre Pronchery     pd->engine = NULL;
149b077aed3SPierre Pronchery }
150b077aed3SPierre Pronchery 
ossl_prov_digest_copy(PROV_DIGEST * dst,const PROV_DIGEST * src)151b077aed3SPierre Pronchery int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
152b077aed3SPierre Pronchery {
153b077aed3SPierre Pronchery     if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
154b077aed3SPierre Pronchery         return 0;
155b077aed3SPierre Pronchery #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156b077aed3SPierre Pronchery     if (src->engine != NULL && !ENGINE_init(src->engine)) {
157b077aed3SPierre Pronchery         EVP_MD_free(src->alloc_md);
158b077aed3SPierre Pronchery         return 0;
159b077aed3SPierre Pronchery     }
160b077aed3SPierre Pronchery #endif
161b077aed3SPierre Pronchery     dst->engine = src->engine;
162b077aed3SPierre Pronchery     dst->md = src->md;
163b077aed3SPierre Pronchery     dst->alloc_md = src->alloc_md;
164b077aed3SPierre Pronchery     return 1;
165b077aed3SPierre Pronchery }
166b077aed3SPierre Pronchery 
ossl_prov_digest_fetch(PROV_DIGEST * pd,OSSL_LIB_CTX * libctx,const char * mdname,const char * propquery)167b077aed3SPierre Pronchery const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
168b077aed3SPierre Pronchery                                      const char *mdname, const char *propquery)
169b077aed3SPierre Pronchery {
170b077aed3SPierre Pronchery     EVP_MD_free(pd->alloc_md);
171b077aed3SPierre Pronchery     pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
172b077aed3SPierre Pronchery 
173b077aed3SPierre Pronchery     return pd->md;
174b077aed3SPierre Pronchery }
175b077aed3SPierre Pronchery 
ossl_prov_digest_load_from_params(PROV_DIGEST * pd,const OSSL_PARAM params[],OSSL_LIB_CTX * ctx)176b077aed3SPierre Pronchery int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177b077aed3SPierre Pronchery                                       const OSSL_PARAM params[],
178b077aed3SPierre Pronchery                                       OSSL_LIB_CTX *ctx)
179b077aed3SPierre Pronchery {
180b077aed3SPierre Pronchery     const OSSL_PARAM *p;
181b077aed3SPierre Pronchery     const char *propquery;
182b077aed3SPierre Pronchery 
183*e7be843bSPierre Pronchery     if (ossl_param_is_empty(params))
184b077aed3SPierre Pronchery         return 1;
185b077aed3SPierre Pronchery 
186b077aed3SPierre Pronchery     if (!load_common(params, &propquery, &pd->engine))
187b077aed3SPierre Pronchery         return 0;
188b077aed3SPierre Pronchery 
189b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190b077aed3SPierre Pronchery     if (p == NULL)
191b077aed3SPierre Pronchery         return 1;
192b077aed3SPierre Pronchery     if (p->data_type != OSSL_PARAM_UTF8_STRING)
193b077aed3SPierre Pronchery         return 0;
194b077aed3SPierre Pronchery 
195b077aed3SPierre Pronchery     ERR_set_mark();
196b077aed3SPierre Pronchery     ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197b077aed3SPierre Pronchery #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198b077aed3SPierre Pronchery     if (pd->md == NULL) {
199b077aed3SPierre Pronchery         const EVP_MD *md;
200b077aed3SPierre Pronchery 
201b077aed3SPierre Pronchery         md = EVP_get_digestbyname(p->data);
202b077aed3SPierre Pronchery         /* Do not use global EVP_MDs */
203b077aed3SPierre Pronchery         if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204b077aed3SPierre Pronchery             pd->md = md;
205b077aed3SPierre Pronchery     }
206b077aed3SPierre Pronchery #endif
207b077aed3SPierre Pronchery     if (pd->md != NULL)
208b077aed3SPierre Pronchery         ERR_pop_to_mark();
209b077aed3SPierre Pronchery     else
210b077aed3SPierre Pronchery         ERR_clear_last_mark();
211b077aed3SPierre Pronchery     return pd->md != NULL;
212b077aed3SPierre Pronchery }
213b077aed3SPierre Pronchery 
ossl_prov_digest_set_md(PROV_DIGEST * pd,EVP_MD * md)214*e7be843bSPierre Pronchery void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md)
215*e7be843bSPierre Pronchery {
216*e7be843bSPierre Pronchery     ossl_prov_digest_reset(pd);
217*e7be843bSPierre Pronchery     pd->md = pd->alloc_md = md;
218*e7be843bSPierre Pronchery }
219*e7be843bSPierre Pronchery 
ossl_prov_digest_md(const PROV_DIGEST * pd)220b077aed3SPierre Pronchery const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
221b077aed3SPierre Pronchery {
222b077aed3SPierre Pronchery     return pd->md;
223b077aed3SPierre Pronchery }
224b077aed3SPierre Pronchery 
ossl_prov_digest_engine(const PROV_DIGEST * pd)225b077aed3SPierre Pronchery ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
226b077aed3SPierre Pronchery {
227b077aed3SPierre Pronchery     return pd->engine;
228b077aed3SPierre Pronchery }
229b077aed3SPierre Pronchery 
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)230b077aed3SPierre Pronchery int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
231b077aed3SPierre Pronchery                          const OSSL_PARAM params[],
232b077aed3SPierre Pronchery                          const char *ciphername,
233b077aed3SPierre Pronchery                          const char *mdname,
234b077aed3SPierre Pronchery                          const char *engine,
235b077aed3SPierre Pronchery                          const char *properties,
236b077aed3SPierre Pronchery                          const unsigned char *key,
237b077aed3SPierre Pronchery                          size_t keylen)
238b077aed3SPierre Pronchery {
239b077aed3SPierre Pronchery     const OSSL_PARAM *p;
240b077aed3SPierre Pronchery     OSSL_PARAM mac_params[6], *mp = mac_params;
241b077aed3SPierre Pronchery 
242b077aed3SPierre Pronchery     if (params != NULL) {
243b077aed3SPierre Pronchery         if (mdname == NULL) {
244b077aed3SPierre Pronchery             if ((p = OSSL_PARAM_locate_const(params,
245b077aed3SPierre Pronchery                                             OSSL_ALG_PARAM_DIGEST)) != NULL) {
246b077aed3SPierre Pronchery                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
247b077aed3SPierre Pronchery                     return 0;
248b077aed3SPierre Pronchery                 mdname = p->data;
249b077aed3SPierre Pronchery             }
250b077aed3SPierre Pronchery         }
251b077aed3SPierre Pronchery         if (ciphername == NULL) {
252b077aed3SPierre Pronchery             if ((p = OSSL_PARAM_locate_const(params,
253b077aed3SPierre Pronchery                                             OSSL_ALG_PARAM_CIPHER)) != NULL) {
254b077aed3SPierre Pronchery                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
255b077aed3SPierre Pronchery                     return 0;
256b077aed3SPierre Pronchery                 ciphername = p->data;
257b077aed3SPierre Pronchery             }
258b077aed3SPierre Pronchery         }
259b077aed3SPierre Pronchery         if (engine == NULL) {
260b077aed3SPierre Pronchery             if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
261b077aed3SPierre Pronchery                     != NULL) {
262b077aed3SPierre Pronchery                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
263b077aed3SPierre Pronchery                     return 0;
264b077aed3SPierre Pronchery                 engine = p->data;
265b077aed3SPierre Pronchery             }
266b077aed3SPierre Pronchery         }
267b077aed3SPierre Pronchery     }
268b077aed3SPierre Pronchery 
269b077aed3SPierre Pronchery     if (mdname != NULL)
270b077aed3SPierre Pronchery         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
271b077aed3SPierre Pronchery                                                  (char *)mdname, 0);
272b077aed3SPierre Pronchery     if (ciphername != NULL)
273b077aed3SPierre Pronchery         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
274b077aed3SPierre Pronchery                                                  (char *)ciphername, 0);
275b077aed3SPierre Pronchery     if (properties != NULL)
276b077aed3SPierre Pronchery         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
277b077aed3SPierre Pronchery                                                  (char *)properties, 0);
278b077aed3SPierre Pronchery 
279b077aed3SPierre Pronchery #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
280b077aed3SPierre Pronchery     if (engine != NULL)
281b077aed3SPierre Pronchery         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
282b077aed3SPierre Pronchery                                                  (char *) engine, 0);
283b077aed3SPierre Pronchery #endif
284b077aed3SPierre Pronchery 
285b077aed3SPierre Pronchery     if (key != NULL)
286b077aed3SPierre Pronchery         *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
287b077aed3SPierre Pronchery                                                   (unsigned char *)key,
288b077aed3SPierre Pronchery                                                   keylen);
289b077aed3SPierre Pronchery 
290b077aed3SPierre Pronchery     *mp = OSSL_PARAM_construct_end();
291b077aed3SPierre Pronchery 
292b077aed3SPierre Pronchery     return EVP_MAC_CTX_set_params(macctx, mac_params);
293b077aed3SPierre Pronchery 
294b077aed3SPierre Pronchery }
295b077aed3SPierre Pronchery 
ossl_prov_macctx_load_from_params(EVP_MAC_CTX ** macctx,const OSSL_PARAM params[],const char * macname,const char * ciphername,const char * mdname,OSSL_LIB_CTX * libctx)296b077aed3SPierre Pronchery int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
297b077aed3SPierre Pronchery                                       const OSSL_PARAM params[],
298b077aed3SPierre Pronchery                                       const char *macname,
299b077aed3SPierre Pronchery                                       const char *ciphername,
300b077aed3SPierre Pronchery                                       const char *mdname,
301b077aed3SPierre Pronchery                                       OSSL_LIB_CTX *libctx)
302b077aed3SPierre Pronchery {
303b077aed3SPierre Pronchery     const OSSL_PARAM *p;
304b077aed3SPierre Pronchery     const char *properties = NULL;
305b077aed3SPierre Pronchery 
306b077aed3SPierre Pronchery     if (macname == NULL
307b077aed3SPierre Pronchery         && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
308b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_UTF8_STRING)
309b077aed3SPierre Pronchery             return 0;
310b077aed3SPierre Pronchery         macname = p->data;
311b077aed3SPierre Pronchery     }
312b077aed3SPierre Pronchery     if ((p = OSSL_PARAM_locate_const(params,
313b077aed3SPierre Pronchery                                      OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
314b077aed3SPierre Pronchery         if (p->data_type != OSSL_PARAM_UTF8_STRING)
315b077aed3SPierre Pronchery             return 0;
316b077aed3SPierre Pronchery         properties = p->data;
317b077aed3SPierre Pronchery     }
318b077aed3SPierre Pronchery 
319b077aed3SPierre Pronchery     /* If we got a new mac name, we make a new EVP_MAC_CTX */
320b077aed3SPierre Pronchery     if (macname != NULL) {
321b077aed3SPierre Pronchery         EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
322b077aed3SPierre Pronchery 
323b077aed3SPierre Pronchery         EVP_MAC_CTX_free(*macctx);
324b077aed3SPierre Pronchery         *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
325b077aed3SPierre Pronchery         /* The context holds on to the MAC */
326b077aed3SPierre Pronchery         EVP_MAC_free(mac);
327b077aed3SPierre Pronchery         if (*macctx == NULL)
328b077aed3SPierre Pronchery             return 0;
329b077aed3SPierre Pronchery     }
330b077aed3SPierre Pronchery 
331b077aed3SPierre Pronchery     /*
332b077aed3SPierre Pronchery      * If there is no MAC yet (and therefore, no MAC context), we ignore
333b077aed3SPierre Pronchery      * all other parameters.
334b077aed3SPierre Pronchery      */
335b077aed3SPierre Pronchery     if (*macctx == NULL)
336b077aed3SPierre Pronchery         return 1;
337b077aed3SPierre Pronchery 
338b077aed3SPierre Pronchery     if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
339b077aed3SPierre Pronchery                              properties, NULL, 0))
340b077aed3SPierre Pronchery         return 1;
341b077aed3SPierre Pronchery 
342b077aed3SPierre Pronchery     EVP_MAC_CTX_free(*macctx);
343b077aed3SPierre Pronchery     *macctx = NULL;
344b077aed3SPierre Pronchery     return 0;
345b077aed3SPierre Pronchery }
346b077aed3SPierre Pronchery 
ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE * in,OSSL_ALGORITHM * out)347b077aed3SPierre Pronchery void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
348b077aed3SPierre Pronchery                                          OSSL_ALGORITHM *out)
349b077aed3SPierre Pronchery {
350b077aed3SPierre Pronchery     int i, j;
351b077aed3SPierre Pronchery 
352b077aed3SPierre Pronchery     if (out[0].algorithm_names == NULL) {
353b077aed3SPierre Pronchery         for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
354b077aed3SPierre Pronchery             if (in[i].capable == NULL || in[i].capable())
355b077aed3SPierre Pronchery                 out[j++] = in[i].alg;
356b077aed3SPierre Pronchery         }
357b077aed3SPierre Pronchery         out[j++] = in[i].alg;
358b077aed3SPierre Pronchery     }
359b077aed3SPierre Pronchery }
360*e7be843bSPierre Pronchery 
361*e7be843bSPierre Pronchery /* Duplicate a lump of memory safely */
ossl_prov_memdup(const void * src,size_t src_len,unsigned char ** dest,size_t * dest_len)362*e7be843bSPierre Pronchery int ossl_prov_memdup(const void *src, size_t src_len,
363*e7be843bSPierre Pronchery                      unsigned char **dest, size_t *dest_len)
364*e7be843bSPierre Pronchery {
365*e7be843bSPierre Pronchery     if (src != NULL) {
366*e7be843bSPierre Pronchery         if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
367*e7be843bSPierre Pronchery             return 0;
368*e7be843bSPierre Pronchery         *dest_len = src_len;
369*e7be843bSPierre Pronchery     } else {
370*e7be843bSPierre Pronchery         *dest = NULL;
371*e7be843bSPierre Pronchery         *dest_len = 0;
372*e7be843bSPierre Pronchery     }
373*e7be843bSPierre Pronchery     return 1;
374*e7be843bSPierre Pronchery }
375