]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/provider.c
Fix memory leak in BN_rand_range()
[thirdparty/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 <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
18 OSSL_PROVIDER *OSSL_PROVIDER_try_load(OSSL_LIB_CTX *libctx, const char *name,
19 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, 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
52 OSSL_PROVIDER *OSSL_PROVIDER_load(OSSL_LIB_CTX *libctx, const char *name)
53 {
54 /* Any attempt to load a provider disables auto-loading of defaults */
55 if (ossl_provider_disable_fallback_loading(libctx))
56 return OSSL_PROVIDER_try_load(libctx, name, 0);
57 return NULL;
58 }
59
60 int OSSL_PROVIDER_unload(OSSL_PROVIDER *prov)
61 {
62 if (!ossl_provider_deactivate(prov, 1))
63 return 0;
64 ossl_provider_free(prov);
65 return 1;
66 }
67
68 const OSSL_PARAM *OSSL_PROVIDER_gettable_params(const OSSL_PROVIDER *prov)
69 {
70 return ossl_provider_gettable_params(prov);
71 }
72
73 int OSSL_PROVIDER_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[])
74 {
75 return ossl_provider_get_params(prov, params);
76 }
77
78 const OSSL_ALGORITHM *OSSL_PROVIDER_query_operation(const OSSL_PROVIDER *prov,
79 int operation_id,
80 int *no_cache)
81 {
82 return ossl_provider_query_operation(prov, operation_id, no_cache);
83 }
84
85 void OSSL_PROVIDER_unquery_operation(const OSSL_PROVIDER *prov,
86 int operation_id,
87 const OSSL_ALGORITHM *algs)
88 {
89 ossl_provider_unquery_operation(prov, operation_id, algs);
90 }
91
92 void *OSSL_PROVIDER_get0_provider_ctx(const OSSL_PROVIDER *prov)
93 {
94 return ossl_provider_prov_ctx(prov);
95 }
96
97 const OSSL_DISPATCH *OSSL_PROVIDER_get0_dispatch(const OSSL_PROVIDER *prov)
98 {
99 return ossl_provider_get0_dispatch(prov);
100 }
101
102 int OSSL_PROVIDER_self_test(const OSSL_PROVIDER *prov)
103 {
104 return ossl_provider_self_test(prov);
105 }
106
107 int OSSL_PROVIDER_get_capabilities(const OSSL_PROVIDER *prov,
108 const char *capability,
109 OSSL_CALLBACK *cb,
110 void *arg)
111 {
112 return ossl_provider_get_capabilities(prov, capability, cb, arg);
113 }
114
115 int OSSL_PROVIDER_add_builtin(OSSL_LIB_CTX *libctx, const char *name,
116 OSSL_provider_init_fn *init_fn)
117 {
118 OSSL_PROVIDER_INFO entry;
119
120 if (name == NULL || init_fn == NULL) {
121 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
122 return 0;
123 }
124 memset(&entry, 0, sizeof(entry));
125 entry.name = OPENSSL_strdup(name);
126 if (entry.name == NULL) {
127 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
128 return 0;
129 }
130 entry.init = init_fn;
131 if (!ossl_provider_info_add_to_store(libctx, &entry)) {
132 ossl_provider_info_clear(&entry);
133 return 0;
134 }
135 return 1;
136 }
137
138 const char *OSSL_PROVIDER_get0_name(const OSSL_PROVIDER *prov)
139 {
140 return ossl_provider_name(prov);
141 }
142
143 int OSSL_PROVIDER_do_all(OSSL_LIB_CTX *ctx,
144 int (*cb)(OSSL_PROVIDER *provider,
145 void *cbdata),
146 void *cbdata)
147 {
148 return ossl_provider_doall_activated(ctx, cb, cbdata);
149 }