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