]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dso/dso_lib.c
Copyright year updates
[thirdparty/openssl.git] / crypto / dso / dso_lib.c
CommitLineData
0f113f3e 1/*
da1c088f 2 * Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
8f4fac7f 3 *
b6a34e9a 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
d2e9e320
RS
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
8f4fac7f
GT
8 */
9
706457b7 10#include "dso_local.h"
cd420b0b 11#include "internal/refcount.h"
8f4fac7f 12
3d8b2ec4 13static DSO *DSO_new_method(DSO_METHOD *meth)
0f113f3e
MC
14{
15 DSO *ret;
16
b51bce94 17 ret = OPENSSL_zalloc(sizeof(*ret));
e077455e 18 if (ret == NULL)
26a7d938 19 return NULL;
0f113f3e
MC
20 ret->meth_data = sk_void_new_null();
21 if (ret->meth_data == NULL) {
22 /* sk_new doesn't generate any errors so we do */
e077455e 23 ERR_raise(ERR_LIB_DSO, ERR_R_CRYPTO_LIB);
0f113f3e 24 OPENSSL_free(ret);
26a7d938 25 return NULL;
0f113f3e 26 }
e6a10b07 27 ret->meth = DSO_METHOD_openssl();
aaab365c 28 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
98637bd3 29 sk_void_free(ret->meth_data);
0f113f3e 30 OPENSSL_free(ret);
c74471d2
AG
31 return NULL;
32 }
33
34 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
35 DSO_free(ret);
0f113f3e
MC
36 ret = NULL;
37 }
c74471d2
AG
38
39 return ret;
0f113f3e 40}
8f4fac7f 41
3d8b2ec4
RS
42DSO *DSO_new(void)
43{
44 return DSO_new_method(NULL);
45}
46
8f4fac7f 47int DSO_free(DSO *dso)
0f113f3e
MC
48{
49 int i;
50
efa7dd64 51 if (dso == NULL)
208fb891 52 return 1;
0f113f3e 53
aaab365c 54 if (CRYPTO_DOWN_REF(&dso->references, &i) <= 0)
c74471d2
AG
55 return 0;
56
f3f1cf84 57 REF_PRINT_COUNT("DSO", dso);
0f113f3e 58 if (i > 0)
c74471d2 59 return 1;
f3f1cf84 60 REF_ASSERT_ISNT(i < 0);
8f4fac7f 61
b39eda7e
MC
62 if ((dso->flags & DSO_FLAG_NO_UNLOAD_ON_FREE) == 0) {
63 if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
9311d0c4 64 ERR_raise(ERR_LIB_DSO, DSO_R_UNLOAD_FAILED);
b39eda7e
MC
65 return 0;
66 }
0f113f3e 67 }
8f4fac7f 68
0f113f3e 69 if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
9311d0c4 70 ERR_raise(ERR_LIB_DSO, DSO_R_FINISH_FAILED);
c74471d2 71 return 0;
0f113f3e
MC
72 }
73
74 sk_void_free(dso->meth_data);
b548a1f1
RS
75 OPENSSL_free(dso->filename);
76 OPENSSL_free(dso->loaded_filename);
aaab365c 77 CRYPTO_FREE_REF(&dso->references);
0f113f3e 78 OPENSSL_free(dso);
c74471d2 79 return 1;
0f113f3e
MC
80}
81
82int DSO_flags(DSO *dso)
83{
84 return ((dso == NULL) ? 0 : dso->flags);
85}
8f4fac7f 86
d9ff8890 87int DSO_up_ref(DSO *dso)
0f113f3e 88{
c74471d2
AG
89 int i;
90
0f113f3e 91 if (dso == NULL) {
9311d0c4 92 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
c74471d2 93 return 0;
0f113f3e 94 }
8f4fac7f 95
aaab365c 96 if (CRYPTO_UP_REF(&dso->references, &i) <= 0)
c74471d2
AG
97 return 0;
98
e26f653d 99 REF_PRINT_COUNT("DSO", dso);
c74471d2
AG
100 REF_ASSERT_ISNT(i < 2);
101 return ((i > 1) ? 1 : 0);
0f113f3e 102}
8f4fac7f 103
b9e63915 104DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
0f113f3e
MC
105{
106 DSO *ret;
107 int allocated = 0;
108
109 if (dso == NULL) {
110 ret = DSO_new_method(meth);
111 if (ret == NULL) {
e077455e 112 ERR_raise(ERR_LIB_DSO, ERR_R_DSO_LIB);
0f113f3e
MC
113 goto err;
114 }
115 allocated = 1;
116 /* Pass the provided flags to the new DSO object */
117 if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
9311d0c4 118 ERR_raise(ERR_LIB_DSO, DSO_R_CTRL_FAILED);
0f113f3e
MC
119 goto err;
120 }
121 } else
122 ret = dso;
123 /* Don't load if we're currently already loaded */
124 if (ret->filename != NULL) {
9311d0c4 125 ERR_raise(ERR_LIB_DSO, DSO_R_DSO_ALREADY_LOADED);
0f113f3e
MC
126 goto err;
127 }
128 /*
129 * filename can only be NULL if we were passed a dso that already has one
130 * set.
131 */
132 if (filename != NULL)
133 if (!DSO_set_filename(ret, filename)) {
9311d0c4 134 ERR_raise(ERR_LIB_DSO, DSO_R_SET_FILENAME_FAILED);
0f113f3e
MC
135 goto err;
136 }
137 filename = ret->filename;
138 if (filename == NULL) {
9311d0c4 139 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
0f113f3e
MC
140 goto err;
141 }
142 if (ret->meth->dso_load == NULL) {
9311d0c4 143 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
0f113f3e
MC
144 goto err;
145 }
146 if (!ret->meth->dso_load(ret)) {
9311d0c4 147 ERR_raise(ERR_LIB_DSO, DSO_R_LOAD_FAILED);
0f113f3e
MC
148 goto err;
149 }
150 /* Load succeeded */
26a7d938 151 return ret;
0f113f3e
MC
152 err:
153 if (allocated)
154 DSO_free(ret);
26a7d938 155 return NULL;
0f113f3e 156}
8f4fac7f 157
e9a68cfb 158DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
0f113f3e
MC
159{
160 DSO_FUNC_TYPE ret = NULL;
161
162 if ((dso == NULL) || (symname == NULL)) {
9311d0c4 163 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 164 return NULL;
0f113f3e
MC
165 }
166 if (dso->meth->dso_bind_func == NULL) {
9311d0c4 167 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
26a7d938 168 return NULL;
0f113f3e
MC
169 }
170 if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
9311d0c4 171 ERR_raise(ERR_LIB_DSO, DSO_R_SYM_FAILURE);
26a7d938 172 return NULL;
0f113f3e
MC
173 }
174 /* Success */
26a7d938 175 return ret;
0f113f3e
MC
176}
177
178/*
179 * I don't really like these *_ctrl functions very much to be perfectly
180 * honest. For one thing, I think I have to return a negative value for any
181 * error because possible DSO_ctrl() commands may return values such as
182 * "size"s that can legitimately be zero (making the standard
61986d32 183 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
0f113f3e
MC
184 * times. I'd prefer "output" values to be passed by reference and the return
185 * value as success/failure like usual ... but we conform when we must... :-)
186 */
b9e63915 187long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
0f113f3e
MC
188{
189 if (dso == NULL) {
9311d0c4 190 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 191 return -1;
0f113f3e
MC
192 }
193 /*
194 * We should intercept certain generic commands and only pass control to
195 * the method-specific ctrl() function if it's something we don't handle.
196 */
197 switch (cmd) {
198 case DSO_CTRL_GET_FLAGS:
199 return dso->flags;
200 case DSO_CTRL_SET_FLAGS:
201 dso->flags = (int)larg;
26a7d938 202 return 0;
0f113f3e
MC
203 case DSO_CTRL_OR_FLAGS:
204 dso->flags |= (int)larg;
26a7d938 205 return 0;
0f113f3e
MC
206 default:
207 break;
208 }
209 if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
9311d0c4 210 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
26a7d938 211 return -1;
0f113f3e 212 }
26a7d938 213 return dso->meth->dso_ctrl(dso, cmd, larg, parg);
0f113f3e 214}
51c8dc37 215
51c8dc37 216const char *DSO_get_filename(DSO *dso)
0f113f3e
MC
217{
218 if (dso == NULL) {
9311d0c4 219 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 220 return NULL;
0f113f3e 221 }
26a7d938 222 return dso->filename;
0f113f3e 223}
51c8dc37
GT
224
225int DSO_set_filename(DSO *dso, const char *filename)
0f113f3e
MC
226{
227 char *copied;
228
229 if ((dso == NULL) || (filename == NULL)) {
9311d0c4 230 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 231 return 0;
0f113f3e
MC
232 }
233 if (dso->loaded_filename) {
9311d0c4 234 ERR_raise(ERR_LIB_DSO, DSO_R_DSO_ALREADY_LOADED);
26a7d938 235 return 0;
0f113f3e
MC
236 }
237 /* We'll duplicate filename */
edae9834 238 copied = OPENSSL_strdup(filename);
e077455e 239 if (copied == NULL)
26a7d938 240 return 0;
b548a1f1 241 OPENSSL_free(dso->filename);
0f113f3e 242 dso->filename = copied;
208fb891 243 return 1;
0f113f3e 244}
51c8dc37 245
cbecb3ac 246char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
0f113f3e
MC
247{
248 char *result = NULL;
249
250 if (dso == NULL || filespec1 == NULL) {
9311d0c4 251 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 252 return NULL;
0f113f3e
MC
253 }
254 if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
255 if (dso->merger != NULL)
256 result = dso->merger(dso, filespec1, filespec2);
257 else if (dso->meth->dso_merger != NULL)
258 result = dso->meth->dso_merger(dso, filespec1, filespec2);
259 }
26a7d938 260 return result;
0f113f3e 261}
cbecb3ac 262
51c8dc37 263char *DSO_convert_filename(DSO *dso, const char *filename)
0f113f3e
MC
264{
265 char *result = NULL;
266
267 if (dso == NULL) {
9311d0c4 268 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
26a7d938 269 return NULL;
0f113f3e
MC
270 }
271 if (filename == NULL)
272 filename = dso->filename;
273 if (filename == NULL) {
9311d0c4 274 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
26a7d938 275 return NULL;
0f113f3e
MC
276 }
277 if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
278 if (dso->name_converter != NULL)
279 result = dso->name_converter(dso, filename);
280 else if (dso->meth->dso_name_converter != NULL)
281 result = dso->meth->dso_name_converter(dso, filename);
282 }
283 if (result == NULL) {
edae9834 284 result = OPENSSL_strdup(filename);
e077455e 285 if (result == NULL)
26a7d938 286 return NULL;
0f113f3e 287 }
26a7d938 288 return result;
0f113f3e 289}
51c8dc37 290
cb6ea61c
MC
291int DSO_pathbyaddr(void *addr, char *path, int sz)
292{
e6a10b07
P
293 DSO_METHOD *meth = DSO_METHOD_openssl();
294
cb6ea61c 295 if (meth->pathbyaddr == NULL) {
9311d0c4 296 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
cb6ea61c
MC
297 return -1;
298 }
299 return (*meth->pathbyaddr) (addr, path, sz);
300}
301
b39eda7e
MC
302DSO *DSO_dsobyaddr(void *addr, int flags)
303{
304 DSO *ret = NULL;
305 char *filename = NULL;
306 int len = DSO_pathbyaddr(addr, NULL, 0);
307
210fe4ed
DG
308 if (len < 0)
309 return NULL;
310
b39eda7e
MC
311 filename = OPENSSL_malloc(len);
312 if (filename != NULL
313 && DSO_pathbyaddr(addr, filename, len) == len)
314 ret = DSO_load(NULL, filename, NULL, flags);
315
316 OPENSSL_free(filename);
317 return ret;
318}
319
c6cb42e4 320void *DSO_global_lookup(const char *name)
0f113f3e 321{
e6a10b07
P
322 DSO_METHOD *meth = DSO_METHOD_openssl();
323
0f113f3e 324 if (meth->globallookup == NULL) {
9311d0c4 325 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
0f113f3e
MC
326 return NULL;
327 }
328 return (*meth->globallookup) (name);
329}