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