/src/openssl/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 | | #include <openssl/evp.h> |
11 | | #include <openssl/core_names.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/proverr.h> |
14 | | #ifndef FIPS_MODULE |
15 | | # include "crypto/evp.h" |
16 | | #endif |
17 | | #include "prov/providercommon.h" |
18 | | #include "prov/provider_util.h" |
19 | | |
20 | | void ossl_prov_cipher_reset(PROV_CIPHER *pc) |
21 | 0 | { |
22 | 0 | EVP_CIPHER_free(pc->alloc_cipher); |
23 | 0 | pc->alloc_cipher = NULL; |
24 | 0 | pc->cipher = NULL; |
25 | 0 | } |
26 | | |
27 | | int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src) |
28 | 0 | { |
29 | 0 | if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher)) |
30 | 0 | return 0; |
31 | 0 | dst->cipher = src->cipher; |
32 | 0 | dst->alloc_cipher = src->alloc_cipher; |
33 | 0 | return 1; |
34 | 0 | } |
35 | | |
36 | | static int set_propq(const OSSL_PARAM *propq, const char **propquery) |
37 | 0 | { |
38 | 0 | *propquery = NULL; |
39 | 0 | if (propq != NULL) { |
40 | 0 | if (propq->data_type != OSSL_PARAM_UTF8_STRING) |
41 | 0 | return 0; |
42 | 0 | *propquery = propq->data; |
43 | 0 | } |
44 | 0 | return 1; |
45 | 0 | } |
46 | | |
47 | | int ossl_prov_cipher_load(PROV_CIPHER *pc, const OSSL_PARAM *cipher, |
48 | | const OSSL_PARAM *propq, OSSL_LIB_CTX *ctx) |
49 | 0 | { |
50 | 0 | const char *propquery; |
51 | |
|
52 | 0 | if (!set_propq(propq, &propquery)) |
53 | 0 | return 0; |
54 | | |
55 | 0 | if (cipher == NULL) |
56 | 0 | return 1; |
57 | 0 | if (cipher->data_type != OSSL_PARAM_UTF8_STRING) |
58 | 0 | return 0; |
59 | | |
60 | 0 | EVP_CIPHER_free(pc->alloc_cipher); |
61 | 0 | ERR_set_mark(); |
62 | 0 | pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, cipher->data, |
63 | 0 | propquery); |
64 | 0 | #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */ |
65 | 0 | if (pc->cipher == NULL) { |
66 | 0 | const EVP_CIPHER *evp_cipher; |
67 | |
|
68 | 0 | evp_cipher = EVP_get_cipherbyname(cipher->data); |
69 | | /* Do not use global EVP_CIPHERs */ |
70 | 0 | if (evp_cipher != NULL && evp_cipher->origin != EVP_ORIG_GLOBAL) |
71 | 0 | pc->cipher = evp_cipher; |
72 | 0 | } |
73 | 0 | #endif |
74 | 0 | if (pc->cipher != NULL) |
75 | 0 | ERR_pop_to_mark(); |
76 | 0 | else |
77 | 0 | ERR_clear_last_mark(); |
78 | 0 | return pc->cipher != NULL; |
79 | 0 | } |
80 | | |
81 | | const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc) |
82 | 0 | { |
83 | 0 | return pc->cipher; |
84 | 0 | } |
85 | | |
86 | | void ossl_prov_digest_reset(PROV_DIGEST *pd) |
87 | 0 | { |
88 | 0 | EVP_MD_free(pd->alloc_md); |
89 | 0 | pd->alloc_md = NULL; |
90 | 0 | pd->md = NULL; |
91 | 0 | } |
92 | | |
93 | | int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src) |
94 | 0 | { |
95 | 0 | if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md)) |
96 | 0 | return 0; |
97 | 0 | dst->md = src->md; |
98 | 0 | dst->alloc_md = src->alloc_md; |
99 | 0 | return 1; |
100 | 0 | } |
101 | | |
102 | | const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx, |
103 | | const char *mdname, const char *propquery) |
104 | 0 | { |
105 | 0 | EVP_MD_free(pd->alloc_md); |
106 | 0 | pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery); |
107 | |
|
108 | 0 | return pd->md; |
109 | 0 | } |
110 | | |
111 | | int ossl_prov_digest_load(PROV_DIGEST *pd, const OSSL_PARAM *digest, |
112 | | const OSSL_PARAM *propq, OSSL_LIB_CTX *ctx) |
113 | 0 | { |
114 | 0 | const char *propquery; |
115 | |
|
116 | 0 | if (!set_propq(propq, &propquery)) |
117 | 0 | return 0; |
118 | | |
119 | 0 | if (digest == NULL) |
120 | 0 | return 1; |
121 | 0 | if (digest->data_type != OSSL_PARAM_UTF8_STRING) |
122 | 0 | return 0; |
123 | | |
124 | 0 | ERR_set_mark(); |
125 | 0 | ossl_prov_digest_fetch(pd, ctx, digest->data, propquery); |
126 | 0 | #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */ |
127 | 0 | if (pd->md == NULL) { |
128 | 0 | const EVP_MD *md; |
129 | |
|
130 | 0 | md = EVP_get_digestbyname(digest->data); |
131 | | /* Do not use global EVP_MDs */ |
132 | 0 | if (md != NULL && md->origin != EVP_ORIG_GLOBAL) |
133 | 0 | pd->md = md; |
134 | 0 | } |
135 | 0 | #endif |
136 | 0 | if (pd->md != NULL) |
137 | 0 | ERR_pop_to_mark(); |
138 | 0 | else |
139 | 0 | ERR_clear_last_mark(); |
140 | 0 | return pd->md != NULL; |
141 | 0 | } |
142 | | |
143 | | void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md) |
144 | 0 | { |
145 | 0 | ossl_prov_digest_reset(pd); |
146 | 0 | pd->md = pd->alloc_md = md; |
147 | 0 | } |
148 | | |
149 | | const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd) |
150 | 0 | { |
151 | 0 | return pd->md; |
152 | 0 | } |
153 | | |
154 | | int ossl_prov_set_macctx(EVP_MAC_CTX *macctx, |
155 | | const char *ciphername, |
156 | | const char *mdname, |
157 | | const char *properties, |
158 | | const OSSL_PARAM param[]) |
159 | 0 | { |
160 | 0 | OSSL_PARAM mac_params[5], *mp = mac_params, *mergep; |
161 | 0 | int free_merge = 0; |
162 | 0 | int ret; |
163 | |
|
164 | 0 | if (mdname != NULL) |
165 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, |
166 | 0 | (char *)mdname, 0); |
167 | 0 | if (ciphername != NULL) |
168 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, |
169 | 0 | (char *)ciphername, 0); |
170 | 0 | if (properties != NULL) |
171 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES, |
172 | 0 | (char *)properties, 0); |
173 | |
|
174 | 0 | *mp = OSSL_PARAM_construct_end(); |
175 | | |
176 | | /* |
177 | | * OSSL_PARAM_merge returns NULL and sets an error if either |
178 | | * list passed to it is NULL, and we aren't guaranteed that the |
179 | | * passed in value of param is not NULL here. |
180 | | * Given that we just want the union of the two lists, even if one |
181 | | * is empty, we have to check for that case, and if param is NULL, |
182 | | * just use the mac_params list. In turn we only free the merge |
183 | | * result if we actually did the merge |
184 | | */ |
185 | 0 | if (param == NULL) { |
186 | 0 | mergep = mac_params; |
187 | 0 | } else { |
188 | 0 | free_merge = 1; |
189 | 0 | mergep = OSSL_PARAM_merge(mac_params, param); |
190 | 0 | if (mergep == NULL) |
191 | 0 | return 0; |
192 | 0 | } |
193 | | |
194 | 0 | ret = EVP_MAC_CTX_set_params(macctx, mergep); |
195 | |
|
196 | 0 | if (free_merge == 1) |
197 | 0 | OSSL_PARAM_free(mergep); |
198 | 0 | return ret; |
199 | 0 | } |
200 | | |
201 | | int ossl_prov_macctx_load(EVP_MAC_CTX **macctx, |
202 | | const OSSL_PARAM *pmac, const OSSL_PARAM *pcipher, |
203 | | const OSSL_PARAM *pdigest, const OSSL_PARAM *propq, |
204 | | const char *macname, const char *ciphername, |
205 | | const char *mdname, OSSL_LIB_CTX *libctx) |
206 | 0 | { |
207 | 0 | const char *properties = NULL; |
208 | |
|
209 | 0 | if (macname == NULL && pmac != NULL) |
210 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pmac, &macname)) |
211 | 0 | return 0; |
212 | 0 | if (propq != NULL && !OSSL_PARAM_get_utf8_string_ptr(propq, &properties)) |
213 | 0 | return 0; |
214 | | |
215 | | /* If we got a new mac name, we make a new EVP_MAC_CTX */ |
216 | 0 | if (macname != NULL) { |
217 | 0 | EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties); |
218 | |
|
219 | 0 | EVP_MAC_CTX_free(*macctx); |
220 | 0 | *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac); |
221 | | /* The context holds on to the MAC */ |
222 | 0 | EVP_MAC_free(mac); |
223 | 0 | if (*macctx == NULL) |
224 | 0 | return 0; |
225 | 0 | } |
226 | | |
227 | | /* |
228 | | * If there is no MAC yet (and therefore, no MAC context), we ignore |
229 | | * all other parameters. |
230 | | */ |
231 | 0 | if (*macctx == NULL) |
232 | 0 | return 1; |
233 | | |
234 | 0 | if (ciphername == NULL && pcipher != NULL) |
235 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pcipher, &ciphername)) |
236 | 0 | return 0; |
237 | 0 | if (mdname == NULL && pdigest != NULL) |
238 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pdigest, &mdname)) |
239 | 0 | return 0; |
240 | | |
241 | 0 | if (ossl_prov_set_macctx(*macctx, ciphername, mdname, properties, NULL)) |
242 | 0 | return 1; |
243 | | |
244 | 0 | EVP_MAC_CTX_free(*macctx); |
245 | 0 | *macctx = NULL; |
246 | 0 | return 0; |
247 | 0 | } |
248 | | |
249 | | void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in, |
250 | | OSSL_ALGORITHM *out) |
251 | 16 | { |
252 | 16 | int i, j; |
253 | | |
254 | 16 | if (out[0].algorithm_names == NULL) { |
255 | 2.24k | for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) { |
256 | 2.22k | if (in[i].capable == NULL || in[i].capable()) |
257 | 2.08k | out[j++] = in[i].alg; |
258 | 2.22k | } |
259 | 16 | out[j++] = in[i].alg; |
260 | 16 | } |
261 | 16 | } |
262 | | |
263 | | /* Duplicate a lump of memory safely */ |
264 | | int ossl_prov_memdup(const void *src, size_t src_len, |
265 | | unsigned char **dest, size_t *dest_len) |
266 | 0 | { |
267 | 0 | if (src != NULL) { |
268 | 0 | if ((*dest = OPENSSL_memdup(src, src_len)) == NULL) |
269 | 0 | return 0; |
270 | 0 | *dest_len = src_len; |
271 | 0 | } else { |
272 | 0 | *dest = NULL; |
273 | 0 | *dest_len = 0; |
274 | 0 | } |
275 | 0 | return 1; |
276 | 0 | } |