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