/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 set_propq(const OSSL_PARAM *propq, const char **propquery) |
52 | 0 | { |
53 | 0 | *propquery = NULL; |
54 | 0 | if (propq != NULL) { |
55 | 0 | if (propq->data_type != OSSL_PARAM_UTF8_STRING) |
56 | 0 | return 0; |
57 | 0 | *propquery = propq->data; |
58 | 0 | } |
59 | 0 | return 1; |
60 | 0 | } |
61 | | |
62 | | static int set_engine(const OSSL_PARAM *e, ENGINE **engine) |
63 | 0 | { |
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 | if (e != NULL) { |
71 | 0 | if (e->data_type != OSSL_PARAM_UTF8_STRING) |
72 | 0 | return 0; |
73 | | /* Get a structural reference */ |
74 | 0 | *engine = ENGINE_by_id(e->data); |
75 | 0 | if (*engine == NULL) |
76 | 0 | return 0; |
77 | | /* Get a functional reference */ |
78 | 0 | if (!ENGINE_init(*engine)) { |
79 | 0 | ENGINE_free(*engine); |
80 | 0 | *engine = NULL; |
81 | 0 | return 0; |
82 | 0 | } |
83 | | /* Free the structural reference */ |
84 | 0 | ENGINE_free(*engine); |
85 | 0 | } |
86 | 0 | #endif |
87 | 0 | return 1; |
88 | 0 | } |
89 | | |
90 | | int ossl_prov_cipher_load(PROV_CIPHER *pc, const OSSL_PARAM *cipher, |
91 | | const OSSL_PARAM *propq, const OSSL_PARAM *engine, |
92 | | OSSL_LIB_CTX *ctx) |
93 | 0 | { |
94 | 0 | const char *propquery; |
95 | |
|
96 | 0 | if (!set_propq(propq, &propquery) || !set_engine(engine, &pc->engine)) |
97 | 0 | return 0; |
98 | | |
99 | 0 | if (cipher == NULL) |
100 | 0 | return 1; |
101 | 0 | if (cipher->data_type != OSSL_PARAM_UTF8_STRING) |
102 | 0 | return 0; |
103 | | |
104 | 0 | EVP_CIPHER_free(pc->alloc_cipher); |
105 | 0 | ERR_set_mark(); |
106 | 0 | pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, cipher->data, |
107 | 0 | propquery); |
108 | 0 | #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */ |
109 | 0 | if (pc->cipher == NULL) { |
110 | 0 | const EVP_CIPHER *evp_cipher; |
111 | |
|
112 | 0 | evp_cipher = EVP_get_cipherbyname(cipher->data); |
113 | | /* Do not use global EVP_CIPHERs */ |
114 | 0 | if (evp_cipher != NULL && evp_cipher->origin != EVP_ORIG_GLOBAL) |
115 | 0 | pc->cipher = evp_cipher; |
116 | 0 | } |
117 | 0 | #endif |
118 | 0 | if (pc->cipher != NULL) |
119 | 0 | ERR_pop_to_mark(); |
120 | 0 | else |
121 | 0 | ERR_clear_last_mark(); |
122 | 0 | return pc->cipher != NULL; |
123 | 0 | } |
124 | | |
125 | | int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc, |
126 | | const OSSL_PARAM params[], |
127 | | OSSL_LIB_CTX *ctx) |
128 | 0 | { |
129 | 0 | return ossl_prov_cipher_load(pc, |
130 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER), |
131 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES), |
132 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE), |
133 | 0 | ctx); |
134 | 0 | } |
135 | | |
136 | | const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc) |
137 | 0 | { |
138 | 0 | return pc->cipher; |
139 | 0 | } |
140 | | |
141 | | ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc) |
142 | 0 | { |
143 | 0 | return pc->engine; |
144 | 0 | } |
145 | | |
146 | | void ossl_prov_digest_reset(PROV_DIGEST *pd) |
147 | 0 | { |
148 | 0 | EVP_MD_free(pd->alloc_md); |
149 | 0 | pd->alloc_md = NULL; |
150 | 0 | pd->md = NULL; |
151 | 0 | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
152 | 0 | ENGINE_finish(pd->engine); |
153 | 0 | #endif |
154 | 0 | pd->engine = NULL; |
155 | 0 | } |
156 | | |
157 | | int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src) |
158 | 0 | { |
159 | 0 | if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md)) |
160 | 0 | return 0; |
161 | 0 | #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE) |
162 | 0 | if (src->engine != NULL && !ENGINE_init(src->engine)) { |
163 | 0 | EVP_MD_free(src->alloc_md); |
164 | 0 | return 0; |
165 | 0 | } |
166 | 0 | #endif |
167 | 0 | dst->engine = src->engine; |
168 | 0 | dst->md = src->md; |
169 | 0 | dst->alloc_md = src->alloc_md; |
170 | 0 | return 1; |
171 | 0 | } |
172 | | |
173 | | const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx, |
174 | | const char *mdname, const char *propquery) |
175 | 0 | { |
176 | 0 | EVP_MD_free(pd->alloc_md); |
177 | 0 | pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery); |
178 | |
|
179 | 0 | return pd->md; |
180 | 0 | } |
181 | | |
182 | | int ossl_prov_digest_load(PROV_DIGEST *pd, const OSSL_PARAM *digest, |
183 | | const OSSL_PARAM *propq, const OSSL_PARAM *engine, |
184 | | OSSL_LIB_CTX *ctx) |
185 | 0 | { |
186 | 0 | const char *propquery; |
187 | |
|
188 | 0 | if (!set_propq(propq, &propquery) || !set_engine(engine, &pd->engine)) |
189 | 0 | return 0; |
190 | | |
191 | 0 | if (digest == NULL) |
192 | 0 | return 1; |
193 | 0 | if (digest->data_type != OSSL_PARAM_UTF8_STRING) |
194 | 0 | return 0; |
195 | | |
196 | 0 | ERR_set_mark(); |
197 | 0 | ossl_prov_digest_fetch(pd, ctx, digest->data, propquery); |
198 | 0 | #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */ |
199 | 0 | if (pd->md == NULL) { |
200 | 0 | const EVP_MD *md; |
201 | |
|
202 | 0 | md = EVP_get_digestbyname(digest->data); |
203 | | /* Do not use global EVP_MDs */ |
204 | 0 | if (md != NULL && md->origin != EVP_ORIG_GLOBAL) |
205 | 0 | pd->md = md; |
206 | 0 | } |
207 | 0 | #endif |
208 | 0 | if (pd->md != NULL) |
209 | 0 | ERR_pop_to_mark(); |
210 | 0 | else |
211 | 0 | ERR_clear_last_mark(); |
212 | 0 | return pd->md != NULL; |
213 | 0 | } |
214 | | |
215 | | int ossl_prov_digest_load_from_params(PROV_DIGEST *pd, |
216 | | const OSSL_PARAM params[], |
217 | | OSSL_LIB_CTX *ctx) |
218 | 0 | { |
219 | 0 | return ossl_prov_digest_load(pd, |
220 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST), |
221 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES), |
222 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE), |
223 | 0 | ctx); |
224 | 0 | } |
225 | | |
226 | | void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md) |
227 | 0 | { |
228 | 0 | ossl_prov_digest_reset(pd); |
229 | 0 | pd->md = pd->alloc_md = md; |
230 | 0 | } |
231 | | |
232 | | const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd) |
233 | 0 | { |
234 | 0 | return pd->md; |
235 | 0 | } |
236 | | |
237 | | ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd) |
238 | 0 | { |
239 | 0 | return pd->engine; |
240 | 0 | } |
241 | | |
242 | | int ossl_prov_set_macctx(EVP_MAC_CTX *macctx, |
243 | | const char *ciphername, |
244 | | const char *mdname, |
245 | | const char *engine, |
246 | | const char *properties) |
247 | 0 | { |
248 | 0 | OSSL_PARAM mac_params[5], *mp = mac_params; |
249 | |
|
250 | 0 | if (mdname != NULL) |
251 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, |
252 | 0 | (char *)mdname, 0); |
253 | 0 | if (ciphername != NULL) |
254 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, |
255 | 0 | (char *)ciphername, 0); |
256 | 0 | if (properties != NULL) |
257 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES, |
258 | 0 | (char *)properties, 0); |
259 | |
|
260 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
261 | 0 | if (engine != NULL) |
262 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE, |
263 | 0 | (char *) engine, 0); |
264 | 0 | #endif |
265 | |
|
266 | 0 | *mp = OSSL_PARAM_construct_end(); |
267 | |
|
268 | 0 | return EVP_MAC_CTX_set_params(macctx, mac_params); |
269 | |
|
270 | 0 | } |
271 | | |
272 | | int ossl_prov_macctx_load(EVP_MAC_CTX **macctx, |
273 | | const OSSL_PARAM *pmac, const OSSL_PARAM *pcipher, |
274 | | const OSSL_PARAM *pdigest, const OSSL_PARAM *propq, |
275 | | const OSSL_PARAM *pengine, |
276 | | const char *macname, const char *ciphername, |
277 | | const char *mdname, OSSL_LIB_CTX *libctx) |
278 | 0 | { |
279 | 0 | const char *properties = NULL; |
280 | 0 | const char *engine = NULL; |
281 | |
|
282 | 0 | if (macname == NULL && pmac != NULL) |
283 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pmac, &macname)) |
284 | 0 | return 0; |
285 | 0 | if (propq != NULL && !OSSL_PARAM_get_utf8_string_ptr(propq, &properties)) |
286 | 0 | return 0; |
287 | | |
288 | | /* If we got a new mac name, we make a new EVP_MAC_CTX */ |
289 | 0 | if (macname != NULL) { |
290 | 0 | EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties); |
291 | |
|
292 | 0 | EVP_MAC_CTX_free(*macctx); |
293 | 0 | *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac); |
294 | | /* The context holds on to the MAC */ |
295 | 0 | EVP_MAC_free(mac); |
296 | 0 | if (*macctx == NULL) |
297 | 0 | return 0; |
298 | 0 | } |
299 | | |
300 | | /* |
301 | | * If there is no MAC yet (and therefore, no MAC context), we ignore |
302 | | * all other parameters. |
303 | | */ |
304 | 0 | if (*macctx == NULL) |
305 | 0 | return 1; |
306 | | |
307 | 0 | if (ciphername == NULL && pcipher != NULL) |
308 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pcipher, &ciphername)) |
309 | 0 | return 0; |
310 | 0 | if (mdname == NULL && pdigest != NULL) |
311 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pdigest, &mdname)) |
312 | 0 | return 0; |
313 | 0 | if (pengine != NULL && !OSSL_PARAM_get_utf8_string_ptr(pengine, &engine)) |
314 | 0 | return 0; |
315 | | |
316 | 0 | if (ossl_prov_set_macctx(*macctx, ciphername, mdname, engine, properties)) |
317 | 0 | return 1; |
318 | | |
319 | 0 | EVP_MAC_CTX_free(*macctx); |
320 | 0 | *macctx = NULL; |
321 | 0 | return 0; |
322 | 0 | } |
323 | | |
324 | | int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx, |
325 | | const OSSL_PARAM params[], |
326 | | const char *macname, |
327 | | const char *ciphername, |
328 | | const char *mdname, |
329 | | OSSL_LIB_CTX *libctx) |
330 | 0 | { |
331 | 0 | return ossl_prov_macctx_load |
332 | 0 | (macctx, OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC), |
333 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER), |
334 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST), |
335 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES), |
336 | 0 | OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE), |
337 | 0 | macname, ciphername, mdname, libctx); |
338 | 0 | } |
339 | | |
340 | | void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in, |
341 | | OSSL_ALGORITHM *out) |
342 | 16 | { |
343 | 16 | int i, j; |
344 | | |
345 | 16 | if (out[0].algorithm_names == NULL) { |
346 | 2.24k | for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) { |
347 | 2.22k | if (in[i].capable == NULL || in[i].capable()) |
348 | 2.08k | out[j++] = in[i].alg; |
349 | 2.22k | } |
350 | 16 | out[j++] = in[i].alg; |
351 | 16 | } |
352 | 16 | } |
353 | | |
354 | | /* Duplicate a lump of memory safely */ |
355 | | int ossl_prov_memdup(const void *src, size_t src_len, |
356 | | unsigned char **dest, size_t *dest_len) |
357 | 0 | { |
358 | 0 | if (src != NULL) { |
359 | 0 | if ((*dest = OPENSSL_memdup(src, src_len)) == NULL) |
360 | 0 | return 0; |
361 | 0 | *dest_len = src_len; |
362 | 0 | } else { |
363 | 0 | *dest = NULL; |
364 | 0 | *dest_len = 0; |
365 | 0 | } |
366 | 0 | return 1; |
367 | 0 | } |