]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/common/provider_util.c
Revert "The EVP_MAC functions have been renamed for consistency. The EVP_MAC_CTX_*"
[thirdparty/openssl.git] / providers / common / provider_util.c
1 /*
2 * Copyright 2019-2020 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 "prov/provider_util.h"
17
18 void ossl_prov_cipher_reset(PROV_CIPHER *pc)
19 {
20 EVP_CIPHER_free(pc->alloc_cipher);
21 pc->alloc_cipher = NULL;
22 pc->cipher = NULL;
23 pc->engine = NULL;
24 }
25
26 int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
27 {
28 if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
29 return 0;
30 dst->engine = src->engine;
31 dst->cipher = src->cipher;
32 dst->alloc_cipher = src->alloc_cipher;
33 return 1;
34 }
35
36 static int load_common(const OSSL_PARAM params[], const char **propquery,
37 ENGINE **engine)
38 {
39 const OSSL_PARAM *p;
40
41 *propquery = NULL;
42 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
43 if (p != NULL) {
44 if (p->data_type != OSSL_PARAM_UTF8_STRING)
45 return 0;
46 *propquery = p->data;
47 }
48
49 *engine = NULL;
50 /* TODO legacy stuff, to be removed */
51 /* Inside the FIPS module, we don't support legacy ciphers */
52 #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
53 p = OSSL_PARAM_locate_const(params, "engine");
54 if (p != NULL) {
55 if (p->data_type != OSSL_PARAM_UTF8_STRING)
56 return 0;
57 ENGINE_finish(*engine);
58 *engine = ENGINE_by_id(p->data);
59 if (*engine == NULL)
60 return 0;
61 }
62 #endif
63 return 1;
64 }
65
66 int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
67 const OSSL_PARAM params[],
68 OPENSSL_CTX *ctx)
69 {
70 const OSSL_PARAM *p;
71 const char *propquery;
72
73 if (!load_common(params, &propquery, &pc->engine))
74 return 0;
75
76 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
77 if (p == NULL)
78 return 1;
79 if (p->data_type != OSSL_PARAM_UTF8_STRING)
80 return 0;
81
82 EVP_CIPHER_free(pc->alloc_cipher);
83 ERR_set_mark();
84 pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
85 /* TODO legacy stuff, to be removed */
86 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
87 if (pc->cipher == NULL)
88 pc->cipher = EVP_get_cipherbyname(p->data);
89 #endif
90 if (pc->cipher != NULL)
91 ERR_pop_to_mark();
92 else
93 ERR_clear_last_mark();
94 return pc->cipher != NULL;
95 }
96
97 const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
98 {
99 return pc->cipher;
100 }
101
102 ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
103 {
104 return pc->engine;
105 }
106
107 void ossl_prov_digest_reset(PROV_DIGEST *pd)
108 {
109 EVP_MD_free(pd->alloc_md);
110 pd->alloc_md = NULL;
111 pd->md = NULL;
112 pd->engine = NULL;
113 }
114
115 int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
116 {
117 if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
118 return 0;
119 dst->engine = src->engine;
120 dst->md = src->md;
121 dst->alloc_md = src->alloc_md;
122 return 1;
123 }
124
125 int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
126 const OSSL_PARAM params[],
127 OPENSSL_CTX *ctx)
128 {
129 const OSSL_PARAM *p;
130 const char *propquery;
131
132 if (!load_common(params, &propquery, &pd->engine))
133 return 0;
134
135
136 p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
137 if (p == NULL)
138 return 1;
139 if (p->data_type != OSSL_PARAM_UTF8_STRING)
140 return 0;
141
142 EVP_MD_free(pd->alloc_md);
143 ERR_set_mark();
144 pd->md = pd->alloc_md = EVP_MD_fetch(ctx, p->data, propquery);
145 /* TODO legacy stuff, to be removed */
146 #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
147 if (pd->md == NULL)
148 pd->md = EVP_get_digestbyname(p->data);
149 #endif
150 if (pd->md != NULL)
151 ERR_pop_to_mark();
152 else
153 ERR_clear_last_mark();
154 return pd->md != NULL;
155 }
156
157 const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
158 {
159 return pd->md;
160 }
161
162 ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
163 {
164 return pd->engine;
165 }
166
167 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
168 const OSSL_PARAM params[],
169 const char *macname,
170 const char *ciphername,
171 const char *mdname,
172 OPENSSL_CTX *libctx)
173 {
174 const OSSL_PARAM *p;
175 OSSL_PARAM mac_params[5], *mp = mac_params;
176 const char *properties = NULL;
177
178 if (macname == NULL
179 && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
180 if (p->data_type != OSSL_PARAM_UTF8_STRING)
181 return 0;
182 macname = p->data;
183 }
184 if ((p = OSSL_PARAM_locate_const(params,
185 OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
186 if (p->data_type != OSSL_PARAM_UTF8_STRING)
187 return 0;
188 properties = p->data;
189 }
190
191 /* If we got a new mac name, we make a new EVP_MAC_CTX */
192 if (macname != NULL) {
193 EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
194
195 EVP_MAC_CTX_free(*macctx);
196 *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
197 /* The context holds on to the MAC */
198 EVP_MAC_free(mac);
199 if (*macctx == NULL)
200 return 0;
201 }
202
203 /*
204 * If there is no MAC yet (and therefore, no MAC context), we ignore
205 * all other parameters.
206 */
207 if (*macctx == NULL)
208 return 1;
209
210 if (mdname == NULL) {
211 if ((p = OSSL_PARAM_locate_const(params,
212 OSSL_ALG_PARAM_DIGEST)) != NULL) {
213 if (p->data_type != OSSL_PARAM_UTF8_STRING)
214 return 0;
215 mdname = p->data;
216 }
217 }
218 if (ciphername == NULL) {
219 if ((p = OSSL_PARAM_locate_const(params,
220 OSSL_ALG_PARAM_CIPHER)) != NULL) {
221 if (p->data_type != OSSL_PARAM_UTF8_STRING)
222 return 0;
223 ciphername = p->data;
224 }
225 }
226
227 if (mdname != NULL)
228 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
229 (char *)mdname, 0);
230 if (ciphername != NULL)
231 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
232 (char *)ciphername, 0);
233 if (properties != NULL)
234 *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
235 (char *)properties, 0);
236
237 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
238 if ((p = OSSL_PARAM_locate_const(params, "engine")) != NULL) {
239 if (p->data_type != OSSL_PARAM_UTF8_STRING)
240 return 0;
241 *mp++ = OSSL_PARAM_construct_utf8_string("engine",
242 p->data, p->data_size);
243 }
244 #endif
245 *mp = OSSL_PARAM_construct_end();
246
247 if (EVP_MAC_CTX_set_params(*macctx, mac_params))
248 return 1;
249
250 EVP_MAC_CTX_free(*macctx);
251 *macctx = NULL;
252 return 0;
253 }
254
255 void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
256 OSSL_ALGORITHM *out)
257 {
258 int i, j;
259
260 if (out[0].algorithm_names == NULL) {
261 for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
262 if (in[i].capable == NULL || in[i].capable())
263 out[j++] = in[i].alg;
264 }
265 out[j++] = in[i].alg;
266 }
267 }