2 * Copyright 2000-2020 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_METHOD
*default_DSO_meth
= NULL
;
15 static DSO
*DSO_new_method(DSO_METHOD
*meth
)
19 if (default_DSO_meth
== NULL
) {
21 * We default to DSO_METH_openssl() which in turn defaults to
22 * stealing the "best available" method. Will fallback to
23 * DSO_METH_null() in the worst case.
25 default_DSO_meth
= DSO_METHOD_openssl();
27 ret
= OPENSSL_zalloc(sizeof(*ret
));
29 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
32 ret
->meth_data
= sk_void_new_null();
33 if (ret
->meth_data
== NULL
) {
34 /* sk_new doesn't generate any errors so we do */
35 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
39 ret
->meth
= default_DSO_meth
;
41 ret
->lock
= CRYPTO_THREAD_lock_new();
42 if (ret
->lock
== NULL
) {
43 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
44 sk_void_free(ret
->meth_data
);
49 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
59 return DSO_new_method(NULL
);
62 int DSO_free(DSO
*dso
)
69 if (CRYPTO_DOWN_REF(&dso
->references
, &i
, dso
->lock
) <= 0)
72 REF_PRINT_COUNT("DSO", dso
);
75 REF_ASSERT_ISNT(i
< 0);
77 if ((dso
->flags
& DSO_FLAG_NO_UNLOAD_ON_FREE
) == 0) {
78 if ((dso
->meth
->dso_unload
!= NULL
) && !dso
->meth
->dso_unload(dso
)) {
79 DSOerr(DSO_F_DSO_FREE
, DSO_R_UNLOAD_FAILED
);
84 if ((dso
->meth
->finish
!= NULL
) && !dso
->meth
->finish(dso
)) {
85 DSOerr(DSO_F_DSO_FREE
, DSO_R_FINISH_FAILED
);
89 sk_void_free(dso
->meth_data
);
90 OPENSSL_free(dso
->filename
);
91 OPENSSL_free(dso
->loaded_filename
);
92 CRYPTO_THREAD_lock_free(dso
->lock
);
97 int DSO_flags(DSO
*dso
)
99 return ((dso
== NULL
) ? 0 : dso
->flags
);
102 int DSO_up_ref(DSO
*dso
)
107 DSOerr(DSO_F_DSO_UP_REF
, ERR_R_PASSED_NULL_PARAMETER
);
111 if (CRYPTO_UP_REF(&dso
->references
, &i
, dso
->lock
) <= 0)
114 REF_PRINT_COUNT("DSO", dso
);
115 REF_ASSERT_ISNT(i
< 2);
116 return ((i
> 1) ? 1 : 0);
119 DSO
*DSO_load(DSO
*dso
, const char *filename
, DSO_METHOD
*meth
, int flags
)
125 ret
= DSO_new_method(meth
);
127 DSOerr(DSO_F_DSO_LOAD
, ERR_R_MALLOC_FAILURE
);
131 /* Pass the provided flags to the new DSO object */
132 if (DSO_ctrl(ret
, DSO_CTRL_SET_FLAGS
, flags
, NULL
) < 0) {
133 DSOerr(DSO_F_DSO_LOAD
, DSO_R_CTRL_FAILED
);
138 /* Don't load if we're currently already loaded */
139 if (ret
->filename
!= NULL
) {
140 DSOerr(DSO_F_DSO_LOAD
, DSO_R_DSO_ALREADY_LOADED
);
144 * filename can only be NULL if we were passed a dso that already has one
147 if (filename
!= NULL
)
148 if (!DSO_set_filename(ret
, filename
)) {
149 DSOerr(DSO_F_DSO_LOAD
, DSO_R_SET_FILENAME_FAILED
);
152 filename
= ret
->filename
;
153 if (filename
== NULL
) {
154 DSOerr(DSO_F_DSO_LOAD
, DSO_R_NO_FILENAME
);
157 if (ret
->meth
->dso_load
== NULL
) {
158 DSOerr(DSO_F_DSO_LOAD
, DSO_R_UNSUPPORTED
);
161 if (!ret
->meth
->dso_load(ret
)) {
162 DSOerr(DSO_F_DSO_LOAD
, DSO_R_LOAD_FAILED
);
173 DSO_FUNC_TYPE
DSO_bind_func(DSO
*dso
, const char *symname
)
175 DSO_FUNC_TYPE ret
= NULL
;
177 if ((dso
== NULL
) || (symname
== NULL
)) {
178 DSOerr(DSO_F_DSO_BIND_FUNC
, ERR_R_PASSED_NULL_PARAMETER
);
181 if (dso
->meth
->dso_bind_func
== NULL
) {
182 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_UNSUPPORTED
);
185 if ((ret
= dso
->meth
->dso_bind_func(dso
, symname
)) == NULL
) {
186 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_SYM_FAILURE
);
194 * I don't really like these *_ctrl functions very much to be perfectly
195 * honest. For one thing, I think I have to return a negative value for any
196 * error because possible DSO_ctrl() commands may return values such as
197 * "size"s that can legitimately be zero (making the standard
198 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
199 * times. I'd prefer "output" values to be passed by reference and the return
200 * value as success/failure like usual ... but we conform when we must... :-)
202 long DSO_ctrl(DSO
*dso
, int cmd
, long larg
, void *parg
)
205 DSOerr(DSO_F_DSO_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
209 * We should intercept certain generic commands and only pass control to
210 * the method-specific ctrl() function if it's something we don't handle.
213 case DSO_CTRL_GET_FLAGS
:
215 case DSO_CTRL_SET_FLAGS
:
216 dso
->flags
= (int)larg
;
218 case DSO_CTRL_OR_FLAGS
:
219 dso
->flags
|= (int)larg
;
224 if ((dso
->meth
== NULL
) || (dso
->meth
->dso_ctrl
== NULL
)) {
225 DSOerr(DSO_F_DSO_CTRL
, DSO_R_UNSUPPORTED
);
228 return dso
->meth
->dso_ctrl(dso
, cmd
, larg
, parg
);
231 const char *DSO_get_filename(DSO
*dso
)
234 DSOerr(DSO_F_DSO_GET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
237 return dso
->filename
;
240 int DSO_set_filename(DSO
*dso
, const char *filename
)
244 if ((dso
== NULL
) || (filename
== NULL
)) {
245 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
248 if (dso
->loaded_filename
) {
249 DSOerr(DSO_F_DSO_SET_FILENAME
, DSO_R_DSO_ALREADY_LOADED
);
252 /* We'll duplicate filename */
253 copied
= OPENSSL_strdup(filename
);
254 if (copied
== NULL
) {
255 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_MALLOC_FAILURE
);
258 OPENSSL_free(dso
->filename
);
259 dso
->filename
= copied
;
263 char *DSO_merge(DSO
*dso
, const char *filespec1
, const char *filespec2
)
267 if (dso
== NULL
|| filespec1
== NULL
) {
268 DSOerr(DSO_F_DSO_MERGE
, ERR_R_PASSED_NULL_PARAMETER
);
271 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
272 if (dso
->merger
!= NULL
)
273 result
= dso
->merger(dso
, filespec1
, filespec2
);
274 else if (dso
->meth
->dso_merger
!= NULL
)
275 result
= dso
->meth
->dso_merger(dso
, filespec1
, filespec2
);
280 char *DSO_convert_filename(DSO
*dso
, const char *filename
)
285 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
288 if (filename
== NULL
)
289 filename
= dso
->filename
;
290 if (filename
== NULL
) {
291 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, DSO_R_NO_FILENAME
);
294 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
295 if (dso
->name_converter
!= NULL
)
296 result
= dso
->name_converter(dso
, filename
);
297 else if (dso
->meth
->dso_name_converter
!= NULL
)
298 result
= dso
->meth
->dso_name_converter(dso
, filename
);
300 if (result
== NULL
) {
301 result
= OPENSSL_strdup(filename
);
302 if (result
== NULL
) {
303 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_MALLOC_FAILURE
);
310 int DSO_pathbyaddr(void *addr
, char *path
, int sz
)
312 DSO_METHOD
*meth
= default_DSO_meth
;
314 meth
= DSO_METHOD_openssl();
315 if (meth
->pathbyaddr
== NULL
) {
316 DSOerr(DSO_F_DSO_PATHBYADDR
, DSO_R_UNSUPPORTED
);
319 return (*meth
->pathbyaddr
) (addr
, path
, sz
);
322 DSO
*DSO_dsobyaddr(void *addr
, int flags
)
325 char *filename
= NULL
;
326 int len
= DSO_pathbyaddr(addr
, NULL
, 0);
331 filename
= OPENSSL_malloc(len
);
333 && DSO_pathbyaddr(addr
, filename
, len
) == len
)
334 ret
= DSO_load(NULL
, filename
, NULL
, flags
);
336 OPENSSL_free(filename
);
340 void *DSO_global_lookup(const char *name
)
342 DSO_METHOD
*meth
= default_DSO_meth
;
344 meth
= DSO_METHOD_openssl();
345 if (meth
->globallookup
== NULL
) {
346 DSOerr(DSO_F_DSO_GLOBAL_LOOKUP
, DSO_R_UNSUPPORTED
);
349 return (*meth
->globallookup
) (name
);