Improve code reuse in the provider MAC bridge
[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, OSSL_ALG_PARAM_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_set_macctx(EVP_MAC_CTX *macctx,
168                          const OSSL_PARAM params[],
169                          const char *ciphername,
170                          const char *mdname,
171                          const char *engine,
172                          const char *properties,
173                          const unsigned char *key,
174                          size_t keylen)
175 {
176     const OSSL_PARAM *p;
177     OSSL_PARAM mac_params[6], *mp = mac_params;
178
179     if (params != NULL) {
180         if (mdname == NULL) {
181             if ((p = OSSL_PARAM_locate_const(params,
182                                             OSSL_ALG_PARAM_DIGEST)) != NULL) {
183                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
184                     return 0;
185                 mdname = p->data;
186             }
187         }
188         if (ciphername == NULL) {
189             if ((p = OSSL_PARAM_locate_const(params,
190                                             OSSL_ALG_PARAM_CIPHER)) != NULL) {
191                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
192                     return 0;
193                 ciphername = p->data;
194             }
195         }
196         if (engine == NULL) {
197             if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
198                     != NULL) {
199                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
200                     return 0;
201                 engine = p->data;
202             }
203         }
204     }
205
206     if (mdname != NULL)
207         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
208                                                  (char *)mdname, 0);
209     if (ciphername != NULL)
210         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
211                                                  (char *)ciphername, 0);
212     if (properties != NULL)
213         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
214                                                  (char *)properties, 0);
215
216 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
217     if (engine != NULL)
218         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
219                                                  (char *) engine, 0);
220 #endif
221
222     if (key != NULL)
223         *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
224                                                   (unsigned char *)key,
225                                                   keylen);
226
227     *mp = OSSL_PARAM_construct_end();
228
229     return EVP_MAC_CTX_set_params(macctx, mac_params);
230
231 }
232
233 int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
234                                       const OSSL_PARAM params[],
235                                       const char *macname,
236                                       const char *ciphername,
237                                       const char *mdname,
238                                       OPENSSL_CTX *libctx)
239 {
240     const OSSL_PARAM *p;
241     const char *properties = NULL;
242
243     if (macname == NULL
244         && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
245         if (p->data_type != OSSL_PARAM_UTF8_STRING)
246             return 0;
247         macname = p->data;
248     }
249     if ((p = OSSL_PARAM_locate_const(params,
250                                      OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
251         if (p->data_type != OSSL_PARAM_UTF8_STRING)
252             return 0;
253         properties = p->data;
254     }
255
256     /* If we got a new mac name, we make a new EVP_MAC_CTX */
257     if (macname != NULL) {
258         EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
259
260         EVP_MAC_CTX_free(*macctx);
261         *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
262         /* The context holds on to the MAC */
263         EVP_MAC_free(mac);
264         if (*macctx == NULL)
265             return 0;
266     }
267
268     /*
269      * If there is no MAC yet (and therefore, no MAC context), we ignore
270      * all other parameters.
271      */
272     if (*macctx == NULL)
273         return 1;
274
275     if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
276                              properties, NULL, 0))
277         return 1;
278
279     EVP_MAC_CTX_free(*macctx);
280     *macctx = NULL;
281     return 0;
282 }
283
284 void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
285                                          OSSL_ALGORITHM *out)
286 {
287     int i, j;
288
289     if (out[0].algorithm_names == NULL) {
290         for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
291             if (in[i].capable == NULL || in[i].capable())
292                 out[j++] = in[i].alg;
293         }
294         out[j++] = in[i].alg;
295     }
296 }