xref: /openssl/crypto/provider.c (revision da1c088f)
1 /*
2  * Copyright 2019-2023 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 #include <string.h>
11 #include <openssl/err.h>
12 #include <openssl/cryptoerr.h>
13 #include <openssl/provider.h>
14 #include <openssl/core_names.h>
15 #include "internal/provider.h"
16 #include "provider_local.h"
17 
OSSL_PROVIDER_try_load_ex(OSSL_LIB_CTX * libctx,const char * name,OSSL_PARAM * params,int retain_fallbacks)18 OSSL_PROVIDER *OSSL_PROVIDER_try_load_ex(OSSL_LIB_CTX *libctx, const char *name,
19                                          OSSL_PARAM *params, int retain_fallbacks)
20 {
21     OSSL_PROVIDER *prov = NULL, *actual;
22     int isnew = 0;
23 
24     /* Find it or create it */
25     if ((prov = ossl_provider_find(libctx, name, 0)) == NULL) {
26         if ((prov = ossl_provider_new(libctx, name, NULL, params, 0)) == NULL)
27             return NULL;
28         isnew = 1;
29     }
30 
31     if (!ossl_provider_activate(prov, 1, 0)) {
32         ossl_provider_free(prov);
33         return NULL;
34     }
35 
36     actual = prov;
37     if (isnew && !ossl_provider_add_to_store(prov, &actual, retain_fallbacks)) {
38         ossl_provider_deactivate(prov, 1);
39         ossl_provider_free(prov);
40         return NULL;
41     }
42     if (actual != prov) {
43         if (!ossl_provider_activate(actual, 1, 0)) {
44             ossl_provider_free(actual);
45             return NULL;
46         }
47     }
48 
49     return actual;
50 }
51 
OSSL_PROVIDER_try_load(OSSL_LIB_CTX * libctx,const char * name,int retain_fallbacks)52 OSSL_PROVIDER *OSSL_PROVIDER_try_load(OSSL_LIB_CTX *libctx, const char *name,
53                                       int retain_fallbacks)
54 {
55     return OSSL_PROVIDER_try_load_ex(libctx, name, NULL, retain_fallbacks);
56 }
57 
OSSL_PROVIDER_load_ex(OSSL_LIB_CTX * libctx,const char * name,OSSL_PARAM * params)58 OSSL_PROVIDER *OSSL_PROVIDER_load_ex(OSSL_LIB_CTX *libctx, const char *name, OSSL_PARAM *params)
59 {
60     /* Any attempt to load a provider disables auto-loading of defaults */
61     if (ossl_provider_disable_fallback_loading(libctx))
62         return OSSL_PROVIDER_try_load_ex(libctx, name, params, 0);
63     return NULL;
64 }
65 
OSSL_PROVIDER_load(OSSL_LIB_CTX * libctx,const char * name)66 OSSL_PROVIDER *OSSL_PROVIDER_load(OSSL_LIB_CTX *libctx, const char *name)
67 {
68     return OSSL_PROVIDER_load_ex(libctx, name, NULL);
69 }
70 
OSSL_PROVIDER_unload(OSSL_PROVIDER * prov)71 int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov)
72 {
73     if (!ossl_provider_deactivate(prov, 1))
74         return 0;
75     ossl_provider_free(prov);
76     return 1;
77 }
78 
OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER * prov)79 const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov)
80 {
81     return ossl_provider_gettable_params(prov);
82 }
83 
OSSL_PROVIDER_get_params(const OSSL_PROVIDER * prov,OSSL_PARAM params[])84 int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
85 {
86     return ossl_provider_get_params(prov, params);
87 }
88 
OSSL_PROVIDER_query_operation(const OSSL_PROVIDER * prov,int operation_id,int * no_cache)89 const OSSL_ALGORITHM *OSSL_PROVIDER_query_operation(const OSSL_PROVIDER *prov,
90                                                     int operation_id,
91                                                     int *no_cache)
92 {
93     return ossl_provider_query_operation(prov, operation_id, no_cache);
94 }
95 
OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER * prov,int operation_id,const OSSL_ALGORITHM * algs)96 void OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER *prov,
97                                      int operation_id,
98                                      const OSSL_ALGORITHM *algs)
99 {
100     ossl_provider_unquery_operation(prov, operation_id, algs);
101 }
102 
OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER * prov)103 void *OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER *prov)
104 {
105     return ossl_provider_prov_ctx(prov);
106 }
107 
OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER * prov)108 const OSSL_DISPATCH *OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER *prov)
109 {
110     return ossl_provider_get0_dispatch(prov);
111 }
112 
OSSL_PROVIDER_self_test(const OSSL_PROVIDER * prov)113 int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov)
114 {
115     return ossl_provider_self_test(prov);
116 }
117 
OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER * prov,const char * capability,OSSL_CALLBACK * cb,void * arg)118 int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov,
119                                    const char *capability,
120                                    OSSL_CALLBACK *cb,
121                                    void *arg)
122 {
123     return ossl_provider_get_capabilities(prov, capability, cb, arg);
124 }
125 
OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX * libctx,const char * name,OSSL_provider_init_fn * init_fn)126 int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
127                               OSSL_provider_init_fn *init_fn)
128 {
129     OSSL_PROVIDER_INFO entry;
130 
131     if (name == NULL || init_fn == NULL) {
132         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
133         return 0;
134     }
135     memset(&entry, 0, sizeof(entry));
136     entry.name = OPENSSL_strdup(name);
137     if (entry.name == NULL)
138         return 0;
139     entry.init = init_fn;
140     if (!ossl_provider_info_add_to_store(libctx, &entry)) {
141         ossl_provider_info_clear(&entry);
142         return 0;
143     }
144     return 1;
145 }
146 
OSSL_PROVIDER_get0_name(const OSSL_PROVIDER * prov)147 const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov)
148 {
149     return ossl_provider_name(prov);
150 }
151 
OSSL_PROVIDER_do_all(OSSL_LIB_CTX * ctx,int (* cb)(OSSL_PROVIDER * provider,void * cbdata),void * cbdata)152 int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx,
153                          int (*cb)(OSSL_PROVIDER *provider,
154                                    void *cbdata),
155                          void *cbdata)
156 {
157     return ossl_provider_doall_activated(ctx, cb, cbdata);
158 }
159