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