xref: /netbsd-src/crypto/external/bsd/openssl/dist/providers/common/provider_util.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*
2*b0d17251Schristos  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos /* We need to use some engine deprecated APIs */
11*b0d17251Schristos #define OPENSSL_SUPPRESS_DEPRECATED
12*b0d17251Schristos 
13*b0d17251Schristos #include <openssl/evp.h>
14*b0d17251Schristos #include <openssl/core_names.h>
15*b0d17251Schristos #include <openssl/err.h>
16*b0d17251Schristos #include <openssl/proverr.h>
17*b0d17251Schristos #ifndef FIPS_MODULE
18*b0d17251Schristos # include <openssl/engine.h>
19*b0d17251Schristos # include "crypto/evp.h"
20*b0d17251Schristos #endif
21*b0d17251Schristos #include "prov/provider_util.h"
22*b0d17251Schristos #include "internal/nelem.h"
23*b0d17251Schristos 
ossl_prov_cipher_reset(PROV_CIPHER * pc)24*b0d17251Schristos void ossl_prov_cipher_reset(PROV_CIPHER *pc)
25*b0d17251Schristos {
26*b0d17251Schristos     EVP_CIPHER_free(pc->alloc_cipher);
27*b0d17251Schristos     pc->alloc_cipher = NULL;
28*b0d17251Schristos     pc->cipher = NULL;
29*b0d17251Schristos #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30*b0d17251Schristos     ENGINE_finish(pc->engine);
31*b0d17251Schristos #endif
32*b0d17251Schristos     pc->engine = NULL;
33*b0d17251Schristos }
34*b0d17251Schristos 
ossl_prov_cipher_copy(PROV_CIPHER * dst,const PROV_CIPHER * src)35*b0d17251Schristos int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
36*b0d17251Schristos {
37*b0d17251Schristos     if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
38*b0d17251Schristos         return 0;
39*b0d17251Schristos #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
40*b0d17251Schristos     if (src->engine != NULL && !ENGINE_init(src->engine)) {
41*b0d17251Schristos         EVP_CIPHER_free(src->alloc_cipher);
42*b0d17251Schristos         return 0;
43*b0d17251Schristos     }
44*b0d17251Schristos #endif
45*b0d17251Schristos     dst->engine = src->engine;
46*b0d17251Schristos     dst->cipher = src->cipher;
47*b0d17251Schristos     dst->alloc_cipher = src->alloc_cipher;
48*b0d17251Schristos     return 1;
49*b0d17251Schristos }
50*b0d17251Schristos 
load_common(const OSSL_PARAM params[],const char ** propquery,ENGINE ** engine)51*b0d17251Schristos static int load_common(const OSSL_PARAM params[], const char **propquery,
52*b0d17251Schristos                        ENGINE **engine)
53*b0d17251Schristos {
54*b0d17251Schristos     const OSSL_PARAM *p;
55*b0d17251Schristos 
56*b0d17251Schristos     *propquery = NULL;
57*b0d17251Schristos     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
58*b0d17251Schristos     if (p != NULL) {
59*b0d17251Schristos         if (p->data_type != OSSL_PARAM_UTF8_STRING)
60*b0d17251Schristos             return 0;
61*b0d17251Schristos         *propquery = p->data;
62*b0d17251Schristos     }
63*b0d17251Schristos 
64*b0d17251Schristos #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65*b0d17251Schristos     ENGINE_finish(*engine);
66*b0d17251Schristos #endif
67*b0d17251Schristos     *engine = NULL;
68*b0d17251Schristos     /* Inside the FIPS module, we don't support legacy ciphers */
69*b0d17251Schristos #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70*b0d17251Schristos     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
71*b0d17251Schristos     if (p != NULL) {
72*b0d17251Schristos         if (p->data_type != OSSL_PARAM_UTF8_STRING)
73*b0d17251Schristos             return 0;
74*b0d17251Schristos         /* Get a structural reference */
75*b0d17251Schristos         *engine = ENGINE_by_id(p->data);
76*b0d17251Schristos         if (*engine == NULL)
77*b0d17251Schristos             return 0;
78*b0d17251Schristos         /* Get a functional reference */
79*b0d17251Schristos         if (!ENGINE_init(*engine)) {
80*b0d17251Schristos             ENGINE_free(*engine);
81*b0d17251Schristos             *engine = NULL;
82*b0d17251Schristos             return 0;
83*b0d17251Schristos         }
84*b0d17251Schristos         /* Free the structural reference */
85*b0d17251Schristos         ENGINE_free(*engine);
86*b0d17251Schristos     }
87*b0d17251Schristos #endif
88*b0d17251Schristos     return 1;
89*b0d17251Schristos }
90*b0d17251Schristos 
ossl_prov_cipher_load_from_params(PROV_CIPHER * pc,const OSSL_PARAM params[],OSSL_LIB_CTX * ctx)91*b0d17251Schristos int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
92*b0d17251Schristos                                       const OSSL_PARAM params[],
93*b0d17251Schristos                                       OSSL_LIB_CTX *ctx)
94*b0d17251Schristos {
95*b0d17251Schristos     const OSSL_PARAM *p;
96*b0d17251Schristos     const char *propquery;
97*b0d17251Schristos 
98*b0d17251Schristos     if (params == NULL)
99*b0d17251Schristos         return 1;
100*b0d17251Schristos 
101*b0d17251Schristos     if (!load_common(params, &propquery, &pc->engine))
102*b0d17251Schristos         return 0;
103*b0d17251Schristos 
104*b0d17251Schristos     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
105*b0d17251Schristos     if (p == NULL)
106*b0d17251Schristos         return 1;
107*b0d17251Schristos     if (p->data_type != OSSL_PARAM_UTF8_STRING)
108*b0d17251Schristos         return 0;
109*b0d17251Schristos 
110*b0d17251Schristos     EVP_CIPHER_free(pc->alloc_cipher);
111*b0d17251Schristos     ERR_set_mark();
112*b0d17251Schristos     pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
113*b0d17251Schristos #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
114*b0d17251Schristos     if (pc->cipher == NULL) {
115*b0d17251Schristos         const EVP_CIPHER *cipher;
116*b0d17251Schristos 
117*b0d17251Schristos         cipher = EVP_get_cipherbyname(p->data);
118*b0d17251Schristos         /* Do not use global EVP_CIPHERs */
119*b0d17251Schristos         if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
120*b0d17251Schristos             pc->cipher = cipher;
121*b0d17251Schristos     }
122*b0d17251Schristos #endif
123*b0d17251Schristos     if (pc->cipher != NULL)
124*b0d17251Schristos         ERR_pop_to_mark();
125*b0d17251Schristos     else
126*b0d17251Schristos         ERR_clear_last_mark();
127*b0d17251Schristos     return pc->cipher != NULL;
128*b0d17251Schristos }
129*b0d17251Schristos 
ossl_prov_cipher_cipher(const PROV_CIPHER * pc)130*b0d17251Schristos const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
131*b0d17251Schristos {
132*b0d17251Schristos     return pc->cipher;
133*b0d17251Schristos }
134*b0d17251Schristos 
ossl_prov_cipher_engine(const PROV_CIPHER * pc)135*b0d17251Schristos ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
136*b0d17251Schristos {
137*b0d17251Schristos     return pc->engine;
138*b0d17251Schristos }
139*b0d17251Schristos 
ossl_prov_digest_reset(PROV_DIGEST * pd)140*b0d17251Schristos void ossl_prov_digest_reset(PROV_DIGEST *pd)
141*b0d17251Schristos {
142*b0d17251Schristos     EVP_MD_free(pd->alloc_md);
143*b0d17251Schristos     pd->alloc_md = NULL;
144*b0d17251Schristos     pd->md = NULL;
145*b0d17251Schristos #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146*b0d17251Schristos     ENGINE_finish(pd->engine);
147*b0d17251Schristos #endif
148*b0d17251Schristos     pd->engine = NULL;
149*b0d17251Schristos }
150*b0d17251Schristos 
ossl_prov_digest_copy(PROV_DIGEST * dst,const PROV_DIGEST * src)151*b0d17251Schristos int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
152*b0d17251Schristos {
153*b0d17251Schristos     if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
154*b0d17251Schristos         return 0;
155*b0d17251Schristos #if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156*b0d17251Schristos     if (src->engine != NULL && !ENGINE_init(src->engine)) {
157*b0d17251Schristos         EVP_MD_free(src->alloc_md);
158*b0d17251Schristos         return 0;
159*b0d17251Schristos     }
160*b0d17251Schristos #endif
161*b0d17251Schristos     dst->engine = src->engine;
162*b0d17251Schristos     dst->md = src->md;
163*b0d17251Schristos     dst->alloc_md = src->alloc_md;
164*b0d17251Schristos     return 1;
165*b0d17251Schristos }
166*b0d17251Schristos 
ossl_prov_digest_fetch(PROV_DIGEST * pd,OSSL_LIB_CTX * libctx,const char * mdname,const char * propquery)167*b0d17251Schristos const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
168*b0d17251Schristos                            const char *mdname, const char *propquery)
169*b0d17251Schristos {
170*b0d17251Schristos     EVP_MD_free(pd->alloc_md);
171*b0d17251Schristos     pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
172*b0d17251Schristos 
173*b0d17251Schristos     return pd->md;
174*b0d17251Schristos }
175*b0d17251Schristos 
ossl_prov_digest_load_from_params(PROV_DIGEST * pd,const OSSL_PARAM params[],OSSL_LIB_CTX * ctx)176*b0d17251Schristos int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177*b0d17251Schristos                                       const OSSL_PARAM params[],
178*b0d17251Schristos                                       OSSL_LIB_CTX *ctx)
179*b0d17251Schristos {
180*b0d17251Schristos     const OSSL_PARAM *p;
181*b0d17251Schristos     const char *propquery;
182*b0d17251Schristos 
183*b0d17251Schristos     if (params == NULL)
184*b0d17251Schristos         return 1;
185*b0d17251Schristos 
186*b0d17251Schristos     if (!load_common(params, &propquery, &pd->engine))
187*b0d17251Schristos         return 0;
188*b0d17251Schristos 
189*b0d17251Schristos     p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190*b0d17251Schristos     if (p == NULL)
191*b0d17251Schristos         return 1;
192*b0d17251Schristos     if (p->data_type != OSSL_PARAM_UTF8_STRING)
193*b0d17251Schristos         return 0;
194*b0d17251Schristos 
195*b0d17251Schristos     ERR_set_mark();
196*b0d17251Schristos     ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197*b0d17251Schristos #ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198*b0d17251Schristos     if (pd->md == NULL) {
199*b0d17251Schristos         const EVP_MD *md;
200*b0d17251Schristos 
201*b0d17251Schristos         md = EVP_get_digestbyname(p->data);
202*b0d17251Schristos         /* Do not use global EVP_MDs */
203*b0d17251Schristos         if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204*b0d17251Schristos             pd->md = md;
205*b0d17251Schristos     }
206*b0d17251Schristos #endif
207*b0d17251Schristos     if (pd->md != NULL)
208*b0d17251Schristos         ERR_pop_to_mark();
209*b0d17251Schristos     else
210*b0d17251Schristos         ERR_clear_last_mark();
211*b0d17251Schristos     return pd->md != NULL;
212*b0d17251Schristos }
213*b0d17251Schristos 
ossl_prov_digest_md(const PROV_DIGEST * pd)214*b0d17251Schristos const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
215*b0d17251Schristos {
216*b0d17251Schristos     return pd->md;
217*b0d17251Schristos }
218*b0d17251Schristos 
ossl_prov_digest_engine(const PROV_DIGEST * pd)219*b0d17251Schristos ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
220*b0d17251Schristos {
221*b0d17251Schristos     return pd->engine;
222*b0d17251Schristos }
223*b0d17251Schristos 
ossl_prov_set_macctx(EVP_MAC_CTX * macctx,const OSSL_PARAM params[],const char * ciphername,const char * mdname,const char * engine,const char * properties,const unsigned char * key,size_t keylen)224*b0d17251Schristos int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
225*b0d17251Schristos                          const OSSL_PARAM params[],
226*b0d17251Schristos                          const char *ciphername,
227*b0d17251Schristos                          const char *mdname,
228*b0d17251Schristos                          const char *engine,
229*b0d17251Schristos                          const char *properties,
230*b0d17251Schristos                          const unsigned char *key,
231*b0d17251Schristos                          size_t keylen)
232*b0d17251Schristos {
233*b0d17251Schristos     const OSSL_PARAM *p;
234*b0d17251Schristos     OSSL_PARAM mac_params[6], *mp = mac_params;
235*b0d17251Schristos 
236*b0d17251Schristos     if (params != NULL) {
237*b0d17251Schristos         if (mdname == NULL) {
238*b0d17251Schristos             if ((p = OSSL_PARAM_locate_const(params,
239*b0d17251Schristos                                             OSSL_ALG_PARAM_DIGEST)) != NULL) {
240*b0d17251Schristos                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
241*b0d17251Schristos                     return 0;
242*b0d17251Schristos                 mdname = p->data;
243*b0d17251Schristos             }
244*b0d17251Schristos         }
245*b0d17251Schristos         if (ciphername == NULL) {
246*b0d17251Schristos             if ((p = OSSL_PARAM_locate_const(params,
247*b0d17251Schristos                                             OSSL_ALG_PARAM_CIPHER)) != NULL) {
248*b0d17251Schristos                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
249*b0d17251Schristos                     return 0;
250*b0d17251Schristos                 ciphername = p->data;
251*b0d17251Schristos             }
252*b0d17251Schristos         }
253*b0d17251Schristos         if (engine == NULL) {
254*b0d17251Schristos             if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
255*b0d17251Schristos                     != NULL) {
256*b0d17251Schristos                 if (p->data_type != OSSL_PARAM_UTF8_STRING)
257*b0d17251Schristos                     return 0;
258*b0d17251Schristos                 engine = p->data;
259*b0d17251Schristos             }
260*b0d17251Schristos         }
261*b0d17251Schristos     }
262*b0d17251Schristos 
263*b0d17251Schristos     if (mdname != NULL)
264*b0d17251Schristos         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
265*b0d17251Schristos                                                  (char *)mdname, 0);
266*b0d17251Schristos     if (ciphername != NULL)
267*b0d17251Schristos         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
268*b0d17251Schristos                                                  (char *)ciphername, 0);
269*b0d17251Schristos     if (properties != NULL)
270*b0d17251Schristos         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
271*b0d17251Schristos                                                  (char *)properties, 0);
272*b0d17251Schristos 
273*b0d17251Schristos #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
274*b0d17251Schristos     if (engine != NULL)
275*b0d17251Schristos         *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
276*b0d17251Schristos                                                  (char *) engine, 0);
277*b0d17251Schristos #endif
278*b0d17251Schristos 
279*b0d17251Schristos     if (key != NULL)
280*b0d17251Schristos         *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
281*b0d17251Schristos                                                   (unsigned char *)key,
282*b0d17251Schristos                                                   keylen);
283*b0d17251Schristos 
284*b0d17251Schristos     *mp = OSSL_PARAM_construct_end();
285*b0d17251Schristos 
286*b0d17251Schristos     return EVP_MAC_CTX_set_params(macctx, mac_params);
287*b0d17251Schristos 
288*b0d17251Schristos }
289*b0d17251Schristos 
ossl_prov_macctx_load_from_params(EVP_MAC_CTX ** macctx,const OSSL_PARAM params[],const char * macname,const char * ciphername,const char * mdname,OSSL_LIB_CTX * libctx)290*b0d17251Schristos int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
291*b0d17251Schristos                                       const OSSL_PARAM params[],
292*b0d17251Schristos                                       const char *macname,
293*b0d17251Schristos                                       const char *ciphername,
294*b0d17251Schristos                                       const char *mdname,
295*b0d17251Schristos                                       OSSL_LIB_CTX *libctx)
296*b0d17251Schristos {
297*b0d17251Schristos     const OSSL_PARAM *p;
298*b0d17251Schristos     const char *properties = NULL;
299*b0d17251Schristos 
300*b0d17251Schristos     if (macname == NULL
301*b0d17251Schristos         && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
302*b0d17251Schristos         if (p->data_type != OSSL_PARAM_UTF8_STRING)
303*b0d17251Schristos             return 0;
304*b0d17251Schristos         macname = p->data;
305*b0d17251Schristos     }
306*b0d17251Schristos     if ((p = OSSL_PARAM_locate_const(params,
307*b0d17251Schristos                                      OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
308*b0d17251Schristos         if (p->data_type != OSSL_PARAM_UTF8_STRING)
309*b0d17251Schristos             return 0;
310*b0d17251Schristos         properties = p->data;
311*b0d17251Schristos     }
312*b0d17251Schristos 
313*b0d17251Schristos     /* If we got a new mac name, we make a new EVP_MAC_CTX */
314*b0d17251Schristos     if (macname != NULL) {
315*b0d17251Schristos         EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
316*b0d17251Schristos 
317*b0d17251Schristos         EVP_MAC_CTX_free(*macctx);
318*b0d17251Schristos         *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
319*b0d17251Schristos         /* The context holds on to the MAC */
320*b0d17251Schristos         EVP_MAC_free(mac);
321*b0d17251Schristos         if (*macctx == NULL)
322*b0d17251Schristos             return 0;
323*b0d17251Schristos     }
324*b0d17251Schristos 
325*b0d17251Schristos     /*
326*b0d17251Schristos      * If there is no MAC yet (and therefore, no MAC context), we ignore
327*b0d17251Schristos      * all other parameters.
328*b0d17251Schristos      */
329*b0d17251Schristos     if (*macctx == NULL)
330*b0d17251Schristos         return 1;
331*b0d17251Schristos 
332*b0d17251Schristos     if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
333*b0d17251Schristos                              properties, NULL, 0))
334*b0d17251Schristos         return 1;
335*b0d17251Schristos 
336*b0d17251Schristos     EVP_MAC_CTX_free(*macctx);
337*b0d17251Schristos     *macctx = NULL;
338*b0d17251Schristos     return 0;
339*b0d17251Schristos }
340*b0d17251Schristos 
ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE * in,OSSL_ALGORITHM * out)341*b0d17251Schristos void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
342*b0d17251Schristos                                          OSSL_ALGORITHM *out)
343*b0d17251Schristos {
344*b0d17251Schristos     int i, j;
345*b0d17251Schristos 
346*b0d17251Schristos     if (out[0].algorithm_names == NULL) {
347*b0d17251Schristos         for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
348*b0d17251Schristos             if (in[i].capable == NULL || in[i].capable())
349*b0d17251Schristos                 out[j++] = in[i].alg;
350*b0d17251Schristos         }
351*b0d17251Schristos         out[j++] = in[i].alg;
352*b0d17251Schristos     }
353*b0d17251Schristos }
354