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