2 * Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "dso_local.h"
11 #include "internal/refcount.h"
13 static DSO *DSO_new_method(DSO_METHOD *meth)
17 ret = OPENSSL_zalloc(sizeof(*ret));
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 */
23 ERR_raise(ERR_LIB_DSO, ERR_R_CRYPTO_LIB);
27 ret->meth = DSO_METHOD_openssl();
28 if (!CRYPTO_NEW_REF(&ret->references, 1)) {
29 sk_void_free(ret->meth_data);
34 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
44 return DSO_new_method(NULL);
47 int DSO_free(DSO *dso)
54 if (CRYPTO_DOWN_REF(&dso->references, &i) <= 0)
57 REF_PRINT_COUNT("DSO", dso);
60 REF_ASSERT_ISNT(i < 0);
62 if ((dso->flags & DSO_FLAG_NO_UNLOAD_ON_FREE) == 0) {
63 if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
64 ERR_raise(ERR_LIB_DSO, DSO_R_UNLOAD_FAILED);
69 if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
70 ERR_raise(ERR_LIB_DSO, DSO_R_FINISH_FAILED);
74 sk_void_free(dso->meth_data);
75 OPENSSL_free(dso->filename);
76 OPENSSL_free(dso->loaded_filename);
77 CRYPTO_FREE_REF(&dso->references);
82 int DSO_flags(DSO *dso)
84 return ((dso == NULL) ? 0 : dso->flags);
87 int DSO_up_ref(DSO *dso)
92 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
96 if (CRYPTO_UP_REF(&dso->references, &i) <= 0)
99 REF_PRINT_COUNT("DSO", dso);
100 REF_ASSERT_ISNT(i < 2);
101 return ((i > 1) ? 1 : 0);
104 DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
110 ret = DSO_new_method(meth);
112 ERR_raise(ERR_LIB_DSO, ERR_R_DSO_LIB);
116 /* Pass the provided flags to the new DSO object */
117 if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
118 ERR_raise(ERR_LIB_DSO, DSO_R_CTRL_FAILED);
123 /* Don't load if we're currently already loaded */
124 if (ret->filename != NULL) {
125 ERR_raise(ERR_LIB_DSO, DSO_R_DSO_ALREADY_LOADED);
129 * filename can only be NULL if we were passed a dso that already has one
132 if (filename != NULL)
133 if (!DSO_set_filename(ret, filename)) {
134 ERR_raise(ERR_LIB_DSO, DSO_R_SET_FILENAME_FAILED);
137 filename = ret->filename;
138 if (filename == NULL) {
139 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
142 if (ret->meth->dso_load == NULL) {
143 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
146 if (!ret->meth->dso_load(ret)) {
147 ERR_raise(ERR_LIB_DSO, DSO_R_LOAD_FAILED);
158 DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
160 DSO_FUNC_TYPE ret = NULL;
162 if ((dso == NULL) || (symname == NULL)) {
163 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
166 if (dso->meth->dso_bind_func == NULL) {
167 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
170 if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
171 ERR_raise(ERR_LIB_DSO, DSO_R_SYM_FAILURE);
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
183 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
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... :-)
187 long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
190 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
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.
198 case DSO_CTRL_GET_FLAGS:
200 case DSO_CTRL_SET_FLAGS:
201 dso->flags = (int)larg;
203 case DSO_CTRL_OR_FLAGS:
204 dso->flags |= (int)larg;
209 if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
210 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
213 return dso->meth->dso_ctrl(dso, cmd, larg, parg);
216 const char *DSO_get_filename(DSO *dso)
219 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
222 return dso->filename;
225 int DSO_set_filename(DSO *dso, const char *filename)
229 if ((dso == NULL) || (filename == NULL)) {
230 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
233 if (dso->loaded_filename) {
234 ERR_raise(ERR_LIB_DSO, DSO_R_DSO_ALREADY_LOADED);
237 /* We'll duplicate filename */
238 copied = OPENSSL_strdup(filename);
241 OPENSSL_free(dso->filename);
242 dso->filename = copied;
246 char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
250 if (dso == NULL || filespec1 == NULL) {
251 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
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);
263 char *DSO_convert_filename(DSO *dso, const char *filename)
268 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
271 if (filename == NULL)
272 filename = dso->filename;
273 if (filename == NULL) {
274 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
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);
283 if (result == NULL) {
284 result = OPENSSL_strdup(filename);
291 int DSO_pathbyaddr(void *addr, char *path, int sz)
293 DSO_METHOD *meth = DSO_METHOD_openssl();
295 if (meth->pathbyaddr == NULL) {
296 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
299 return (*meth->pathbyaddr) (addr, path, sz);
302 DSO *DSO_dsobyaddr(void *addr, int flags)
305 char *filename = NULL;
306 int len = DSO_pathbyaddr(addr, NULL, 0);
311 filename = OPENSSL_malloc(len);
313 && DSO_pathbyaddr(addr, filename, len) == len)
314 ret = DSO_load(NULL, filename, NULL, flags);
316 OPENSSL_free(filename);
320 void *DSO_global_lookup(const char *name)
322 DSO_METHOD *meth = DSO_METHOD_openssl();
324 if (meth->globallookup == NULL) {
325 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
328 return (*meth->globallookup) (name);