2 * Copyright 2000-2017 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"
15 static DSO_METHOD
*default_DSO_meth
= NULL
;
17 static DSO
*DSO_new_method(DSO_METHOD
*meth
)
21 if (default_DSO_meth
== NULL
) {
23 * We default to DSO_METH_openssl() which in turn defaults to
24 * stealing the "best available" method. Will fallback to
25 * DSO_METH_null() in the worst case.
27 default_DSO_meth
= DSO_METHOD_openssl();
29 ret
= OPENSSL_zalloc(sizeof(*ret
));
31 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
34 ret
->meth_data
= sk_void_new_null();
35 if (ret
->meth_data
== NULL
) {
36 /* sk_new doesn't generate any errors so we do */
37 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
41 ret
->meth
= default_DSO_meth
;
43 ret
->lock
= CRYPTO_THREAD_lock_new();
44 if (ret
->lock
== NULL
) {
45 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
46 sk_void_free(ret
->meth_data
);
51 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
61 return DSO_new_method(NULL
);
64 int DSO_free(DSO
*dso
)
71 if (CRYPTO_DOWN_REF(&dso
->references
, &i
, dso
->lock
) <= 0)
74 REF_PRINT_COUNT("DSO", dso
);
77 REF_ASSERT_ISNT(i
< 0);
79 if ((dso
->flags
& DSO_FLAG_NO_UNLOAD_ON_FREE
) == 0) {
80 if ((dso
->meth
->dso_unload
!= NULL
) && !dso
->meth
->dso_unload(dso
)) {
81 DSOerr(DSO_F_DSO_FREE
, DSO_R_UNLOAD_FAILED
);
86 if ((dso
->meth
->finish
!= NULL
) && !dso
->meth
->finish(dso
)) {
87 DSOerr(DSO_F_DSO_FREE
, DSO_R_FINISH_FAILED
);
91 sk_void_free(dso
->meth_data
);
92 OPENSSL_free(dso
->filename
);
93 OPENSSL_free(dso
->loaded_filename
);
94 CRYPTO_THREAD_lock_free(dso
->lock
);
99 int DSO_flags(DSO
*dso
)
101 return ((dso
== NULL
) ? 0 : dso
->flags
);
104 int DSO_up_ref(DSO
*dso
)
109 DSOerr(DSO_F_DSO_UP_REF
, ERR_R_PASSED_NULL_PARAMETER
);
113 if (CRYPTO_UP_REF(&dso
->references
, &i
, dso
->lock
) <= 0)
116 REF_PRINT_COUNT("DSO", dso
);
117 REF_ASSERT_ISNT(i
< 2);
118 return ((i
> 1) ? 1 : 0);
121 DSO
*DSO_load(DSO
*dso
, const char *filename
, DSO_METHOD
*meth
, int flags
)
127 ret
= DSO_new_method(meth
);
129 DSOerr(DSO_F_DSO_LOAD
, ERR_R_MALLOC_FAILURE
);
133 /* Pass the provided flags to the new DSO object */
134 if (DSO_ctrl(ret
, DSO_CTRL_SET_FLAGS
, flags
, NULL
) < 0) {
135 DSOerr(DSO_F_DSO_LOAD
, DSO_R_CTRL_FAILED
);
140 /* Don't load if we're currently already loaded */
141 if (ret
->filename
!= NULL
) {
142 DSOerr(DSO_F_DSO_LOAD
, DSO_R_DSO_ALREADY_LOADED
);
146 * filename can only be NULL if we were passed a dso that already has one
149 if (filename
!= NULL
)
150 if (!DSO_set_filename(ret
, filename
)) {
151 DSOerr(DSO_F_DSO_LOAD
, DSO_R_SET_FILENAME_FAILED
);
154 filename
= ret
->filename
;
155 if (filename
== NULL
) {
156 DSOerr(DSO_F_DSO_LOAD
, DSO_R_NO_FILENAME
);
159 if (ret
->meth
->dso_load
== NULL
) {
160 DSOerr(DSO_F_DSO_LOAD
, DSO_R_UNSUPPORTED
);
163 if (!ret
->meth
->dso_load(ret
)) {
164 DSOerr(DSO_F_DSO_LOAD
, DSO_R_LOAD_FAILED
);
175 DSO_FUNC_TYPE
DSO_bind_func(DSO
*dso
, const char *symname
)
177 DSO_FUNC_TYPE ret
= NULL
;
179 if ((dso
== NULL
) || (symname
== NULL
)) {
180 DSOerr(DSO_F_DSO_BIND_FUNC
, ERR_R_PASSED_NULL_PARAMETER
);
183 if (dso
->meth
->dso_bind_func
== NULL
) {
184 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_UNSUPPORTED
);
187 if ((ret
= dso
->meth
->dso_bind_func(dso
, symname
)) == NULL
) {
188 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_SYM_FAILURE
);
196 * I don't really like these *_ctrl functions very much to be perfectly
197 * honest. For one thing, I think I have to return a negative value for any
198 * error because possible DSO_ctrl() commands may return values such as
199 * "size"s that can legitimately be zero (making the standard
200 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
201 * times. I'd prefer "output" values to be passed by reference and the return
202 * value as success/failure like usual ... but we conform when we must... :-)
204 long DSO_ctrl(DSO
*dso
, int cmd
, long larg
, void *parg
)
207 DSOerr(DSO_F_DSO_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
211 * We should intercept certain generic commands and only pass control to
212 * the method-specific ctrl() function if it's something we don't handle.
215 case DSO_CTRL_GET_FLAGS
:
217 case DSO_CTRL_SET_FLAGS
:
218 dso
->flags
= (int)larg
;
220 case DSO_CTRL_OR_FLAGS
:
221 dso
->flags
|= (int)larg
;
226 if ((dso
->meth
== NULL
) || (dso
->meth
->dso_ctrl
== NULL
)) {
227 DSOerr(DSO_F_DSO_CTRL
, DSO_R_UNSUPPORTED
);
230 return dso
->meth
->dso_ctrl(dso
, cmd
, larg
, parg
);
233 const char *DSO_get_filename(DSO
*dso
)
236 DSOerr(DSO_F_DSO_GET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
239 return dso
->filename
;
242 int DSO_set_filename(DSO
*dso
, const char *filename
)
246 if ((dso
== NULL
) || (filename
== NULL
)) {
247 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
250 if (dso
->loaded_filename
) {
251 DSOerr(DSO_F_DSO_SET_FILENAME
, DSO_R_DSO_ALREADY_LOADED
);
254 /* We'll duplicate filename */
255 copied
= OPENSSL_strdup(filename
);
256 if (copied
== NULL
) {
257 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_MALLOC_FAILURE
);
260 OPENSSL_free(dso
->filename
);
261 dso
->filename
= copied
;
265 char *DSO_merge(DSO
*dso
, const char *filespec1
, const char *filespec2
)
269 if (dso
== NULL
|| filespec1
== NULL
) {
270 DSOerr(DSO_F_DSO_MERGE
, ERR_R_PASSED_NULL_PARAMETER
);
273 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
274 if (dso
->merger
!= NULL
)
275 result
= dso
->merger(dso
, filespec1
, filespec2
);
276 else if (dso
->meth
->dso_merger
!= NULL
)
277 result
= dso
->meth
->dso_merger(dso
, filespec1
, filespec2
);
282 char *DSO_convert_filename(DSO
*dso
, const char *filename
)
287 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
290 if (filename
== NULL
)
291 filename
= dso
->filename
;
292 if (filename
== NULL
) {
293 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, DSO_R_NO_FILENAME
);
296 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
297 if (dso
->name_converter
!= NULL
)
298 result
= dso
->name_converter(dso
, filename
);
299 else if (dso
->meth
->dso_name_converter
!= NULL
)
300 result
= dso
->meth
->dso_name_converter(dso
, filename
);
302 if (result
== NULL
) {
303 result
= OPENSSL_strdup(filename
);
304 if (result
== NULL
) {
305 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_MALLOC_FAILURE
);
312 int DSO_pathbyaddr(void *addr
, char *path
, int sz
)
314 DSO_METHOD
*meth
= default_DSO_meth
;
316 meth
= DSO_METHOD_openssl();
317 if (meth
->pathbyaddr
== NULL
) {
318 DSOerr(DSO_F_DSO_PATHBYADDR
, DSO_R_UNSUPPORTED
);
321 return (*meth
->pathbyaddr
) (addr
, path
, sz
);
324 DSO
*DSO_dsobyaddr(void *addr
, int flags
)
327 char *filename
= NULL
;
328 int len
= DSO_pathbyaddr(addr
, NULL
, 0);
333 filename
= OPENSSL_malloc(len
);
335 && DSO_pathbyaddr(addr
, filename
, len
) == len
)
336 ret
= DSO_load(NULL
, filename
, NULL
, flags
);
338 OPENSSL_free(filename
);
342 void *DSO_global_lookup(const char *name
)
344 DSO_METHOD
*meth
= default_DSO_meth
;
346 meth
= DSO_METHOD_openssl();
347 if (meth
->globallookup
== NULL
) {
348 DSOerr(DSO_F_DSO_GLOBAL_LOOKUP
, DSO_R_UNSUPPORTED
);
351 return (*meth
->globallookup
) (name
);