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