Replace non-ASCII character in source file
[openssl.git] / crypto / provider.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 #include <openssl/err.h>
11 #include <openssl/cryptoerr.h>
12 #include <openssl/provider.h>
13 #include <openssl/core_names.h>
14 #include "internal/provider.h"
15
16 OSSL_PROVIDER *OSSL_PROVIDER_try_load(OSSL_LIB_CTX *libctx, const char *name,
17                                       int retain_fallbacks)
18 {
19     OSSL_PROVIDER *prov = NULL;
20
21     /* Find it or create it */
22     if ((prov = ossl_provider_find(libctx, name, 0)) == NULL
23         && (prov = ossl_provider_new(libctx, name, NULL, 0)) == NULL)
24         return NULL;
25
26     if (!ossl_provider_activate(prov, retain_fallbacks, 1)) {
27         ossl_provider_free(prov);
28         return NULL;
29     }
30
31     return prov;
32 }
33
34 OSSL_PROVIDER *OSSL_PROVIDER_load(OSSL_LIB_CTX *libctx, const char *name)
35 {
36     /* Any attempt to load a provider disables auto-loading of defaults */
37     if (ossl_provider_disable_fallback_loading(libctx))
38         return OSSL_PROVIDER_try_load(libctx, name, 0);
39     return NULL;
40 }
41
42 int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov)
43 {
44     if (!ossl_provider_deactivate(prov))
45         return 0;
46     ossl_provider_free(prov);
47     return 1;
48 }
49
50 int OSSL_PROVIDER_available(OSSL_LIB_CTX *libctx, const char *name)
51 {
52     OSSL_PROVIDER *prov = NULL;
53     int available = 0;
54
55     /* Find it or create it */
56     prov = ossl_provider_find(libctx, name, 0);
57     available = ossl_provider_available(prov);
58     ossl_provider_free(prov);
59     return available;
60 }
61
62 const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov)
63 {
64     return ossl_provider_gettable_params(prov);
65 }
66
67 int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
68 {
69     return ossl_provider_get_params(prov, params);
70 }
71
72 const OSSL_ALGORITHM *OSSL_PROVIDER_query_operation(const OSSL_PROVIDER *prov,
73                                                     int operation_id,
74                                                     int *no_cache)
75 {
76     return ossl_provider_query_operation(prov, operation_id, no_cache);
77 }
78
79 void OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER *prov,
80                                      int operation_id,
81                                      const OSSL_ALGORITHM *algs)
82 {
83     ossl_provider_unquery_operation(prov, operation_id, algs);
84 }
85
86 void *OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER *prov)
87 {
88     return ossl_provider_prov_ctx(prov);
89 }
90
91 const OSSL_DISPATCH *OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER *prov)
92 {
93     return ossl_provider_get0_dispatch(prov);
94 }
95
96 int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov)
97 {
98     return ossl_provider_self_test(prov);
99 }
100
101 int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov,
102                                    const char *capability,
103                                    OSSL_CALLBACK *cb,
104                                    void *arg)
105 {
106     return ossl_provider_get_capabilities(prov, capability, cb, arg);
107 }
108
109 int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
110                               OSSL_provider_init_fn *init_fn)
111 {
112     OSSL_PROVIDER *prov = NULL;
113
114     if (name == NULL || init_fn == NULL) {
115         ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
116         return 0;
117     }
118
119     /* Create it */
120     if ((prov = ossl_provider_new(libctx, name, init_fn, 0)) == NULL)
121         return 0;
122
123     /*
124      * It's safely stored in the internal store at this point,
125      * free the returned extra reference
126      */
127     ossl_provider_free(prov);
128
129     return 1;
130 }
131
132 const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov)
133 {
134     return ossl_provider_name(prov);
135 }
136
137 int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx,
138                          int (*cb)(OSSL_PROVIDER *provider,
139                                    void *cbdata),
140                          void *cbdata)
141 {
142     return ossl_provider_doall_activated(ctx, cb, cbdata);
143 }