/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 | pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, cipher->data, |
62 | 0 | propquery); |
63 | 0 | return pc->cipher != NULL; |
64 | 0 | } |
65 | | |
66 | | const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc) |
67 | 0 | { |
68 | 0 | return pc->cipher; |
69 | 0 | } |
70 | | |
71 | | void ossl_prov_digest_reset(PROV_DIGEST *pd) |
72 | 0 | { |
73 | 0 | EVP_MD_free(pd->alloc_md); |
74 | 0 | pd->alloc_md = NULL; |
75 | 0 | pd->md = NULL; |
76 | 0 | } |
77 | | |
78 | | int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src) |
79 | 0 | { |
80 | 0 | if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md)) |
81 | 0 | return 0; |
82 | 0 | dst->md = src->md; |
83 | 0 | dst->alloc_md = src->alloc_md; |
84 | 0 | return 1; |
85 | 0 | } |
86 | | |
87 | | const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx, |
88 | | const char *mdname, const char *propquery) |
89 | 0 | { |
90 | 0 | EVP_MD_free(pd->alloc_md); |
91 | 0 | pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery); |
92 | |
|
93 | 0 | return pd->md; |
94 | 0 | } |
95 | | |
96 | | int ossl_prov_digest_load(PROV_DIGEST *pd, const OSSL_PARAM *digest, |
97 | | const OSSL_PARAM *propq, OSSL_LIB_CTX *ctx) |
98 | 0 | { |
99 | 0 | const char *propquery; |
100 | |
|
101 | 0 | if (!set_propq(propq, &propquery)) |
102 | 0 | return 0; |
103 | | |
104 | 0 | if (digest == NULL) |
105 | 0 | return 1; |
106 | 0 | if (digest->data_type != OSSL_PARAM_UTF8_STRING) |
107 | 0 | return 0; |
108 | | |
109 | 0 | ossl_prov_digest_fetch(pd, ctx, digest->data, propquery); |
110 | 0 | return pd->md != NULL; |
111 | 0 | } |
112 | | |
113 | | void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md) |
114 | 0 | { |
115 | 0 | ossl_prov_digest_reset(pd); |
116 | 0 | pd->md = pd->alloc_md = md; |
117 | 0 | } |
118 | | |
119 | | const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd) |
120 | 0 | { |
121 | 0 | return pd->md; |
122 | 0 | } |
123 | | |
124 | | int ossl_prov_set_macctx(EVP_MAC_CTX *macctx, |
125 | | const char *ciphername, |
126 | | const char *mdname, |
127 | | const char *properties, |
128 | | const OSSL_PARAM param[]) |
129 | 0 | { |
130 | 0 | OSSL_PARAM mac_params[5], *mp = mac_params, *mergep; |
131 | 0 | int free_merge = 0; |
132 | 0 | int ret; |
133 | |
|
134 | 0 | if (mdname != NULL) |
135 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, |
136 | 0 | (char *)mdname, 0); |
137 | 0 | if (ciphername != NULL) |
138 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER, |
139 | 0 | (char *)ciphername, 0); |
140 | 0 | if (properties != NULL) |
141 | 0 | *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES, |
142 | 0 | (char *)properties, 0); |
143 | |
|
144 | 0 | *mp = OSSL_PARAM_construct_end(); |
145 | | |
146 | | /* |
147 | | * OSSL_PARAM_merge returns NULL and sets an error if either |
148 | | * list passed to it is NULL, and we aren't guaranteed that the |
149 | | * passed in value of param is not NULL here. |
150 | | * Given that we just want the union of the two lists, even if one |
151 | | * is empty, we have to check for that case, and if param is NULL, |
152 | | * just use the mac_params list. In turn we only free the merge |
153 | | * result if we actually did the merge |
154 | | */ |
155 | 0 | if (param == NULL) { |
156 | 0 | mergep = mac_params; |
157 | 0 | } else { |
158 | 0 | free_merge = 1; |
159 | 0 | mergep = OSSL_PARAM_merge(mac_params, param); |
160 | 0 | if (mergep == NULL) |
161 | 0 | return 0; |
162 | 0 | } |
163 | | |
164 | 0 | ret = EVP_MAC_CTX_set_params(macctx, mergep); |
165 | |
|
166 | 0 | if (free_merge == 1) |
167 | 0 | OSSL_PARAM_free(mergep); |
168 | 0 | return ret; |
169 | 0 | } |
170 | | |
171 | | int ossl_prov_macctx_load(EVP_MAC_CTX **macctx, |
172 | | const OSSL_PARAM *pmac, const OSSL_PARAM *pcipher, |
173 | | const OSSL_PARAM *pdigest, const OSSL_PARAM *propq, |
174 | | const char *macname, const char *ciphername, |
175 | | const char *mdname, OSSL_LIB_CTX *libctx) |
176 | 0 | { |
177 | 0 | const char *properties = NULL; |
178 | |
|
179 | 0 | if (macname == NULL && pmac != NULL) |
180 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pmac, &macname)) |
181 | 0 | return 0; |
182 | 0 | if (propq != NULL && !OSSL_PARAM_get_utf8_string_ptr(propq, &properties)) |
183 | 0 | return 0; |
184 | | |
185 | | /* If we got a new mac name, we make a new EVP_MAC_CTX */ |
186 | 0 | if (macname != NULL) { |
187 | 0 | EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties); |
188 | |
|
189 | 0 | EVP_MAC_CTX_free(*macctx); |
190 | 0 | *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac); |
191 | | /* The context holds on to the MAC */ |
192 | 0 | EVP_MAC_free(mac); |
193 | 0 | if (*macctx == NULL) |
194 | 0 | return 0; |
195 | 0 | } |
196 | | |
197 | | /* |
198 | | * If there is no MAC yet (and therefore, no MAC context), we ignore |
199 | | * all other parameters. |
200 | | */ |
201 | 0 | if (*macctx == NULL) |
202 | 0 | return 1; |
203 | | |
204 | 0 | if (ciphername == NULL && pcipher != NULL) |
205 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pcipher, &ciphername)) |
206 | 0 | return 0; |
207 | 0 | if (mdname == NULL && pdigest != NULL) |
208 | 0 | if (!OSSL_PARAM_get_utf8_string_ptr(pdigest, &mdname)) |
209 | 0 | return 0; |
210 | | |
211 | 0 | if (ossl_prov_set_macctx(*macctx, ciphername, mdname, properties, NULL)) |
212 | 0 | return 1; |
213 | | |
214 | 0 | EVP_MAC_CTX_free(*macctx); |
215 | 0 | *macctx = NULL; |
216 | 0 | return 0; |
217 | 0 | } |
218 | | |
219 | | void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in, |
220 | | OSSL_ALGORITHM *out) |
221 | 4 | { |
222 | 4 | int i, j; |
223 | | |
224 | 4 | if (out[0].algorithm_names == NULL) { |
225 | 420 | for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) { |
226 | 417 | if (in[i].capable == NULL || in[i].capable()) |
227 | 378 | out[j++] = in[i].alg; |
228 | 417 | } |
229 | 3 | out[j++] = in[i].alg; |
230 | 3 | } |
231 | 4 | } |
232 | | |
233 | | /* Duplicate a lump of memory safely */ |
234 | | int ossl_prov_memdup(const void *src, size_t src_len, |
235 | | unsigned char **dest, size_t *dest_len) |
236 | 0 | { |
237 | 0 | if (src != NULL) { |
238 | 0 | if ((*dest = OPENSSL_memdup(src, src_len)) == NULL) |
239 | 0 | return 0; |
240 | 0 | *dest_len = src_len; |
241 | 0 | } else { |
242 | 0 | *dest = NULL; |
243 | 0 | *dest_len = 0; |
244 | 0 | } |
245 | 0 | return 1; |
246 | 0 | } |