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