2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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
12 static DSO_METHOD
*default_DSO_meth
= NULL
;
14 static DSO
*DSO_new_method(DSO_METHOD
*meth
)
18 if (default_DSO_meth
== NULL
) {
20 * We default to DSO_METH_openssl() which in turn defaults to
21 * stealing the "best available" method. Will fallback to
22 * DSO_METH_null() in the worst case.
24 default_DSO_meth
= DSO_METHOD_openssl();
26 ret
= OPENSSL_zalloc(sizeof(*ret
));
28 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
31 ret
->meth_data
= sk_void_new_null();
32 if (ret
->meth_data
== NULL
) {
33 /* sk_new doesn't generate any errors so we do */
34 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
38 ret
->meth
= default_DSO_meth
;
40 ret
->lock
= CRYPTO_THREAD_lock_new();
41 if (ret
->lock
== NULL
) {
42 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
43 sk_void_free(ret
->meth_data
);
48 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
58 return DSO_new_method(NULL
);
61 int DSO_free(DSO
*dso
)
68 if (CRYPTO_DOWN_REF(&dso
->references
, &i
, dso
->lock
) <= 0)
71 REF_PRINT_COUNT("DSO", dso
);
74 REF_ASSERT_ISNT(i
< 0);
76 if ((dso
->flags
& DSO_FLAG_NO_UNLOAD_ON_FREE
) == 0) {
77 if ((dso
->meth
->dso_unload
!= NULL
) && !dso
->meth
->dso_unload(dso
)) {
78 DSOerr(DSO_F_DSO_FREE
, DSO_R_UNLOAD_FAILED
);
83 if ((dso
->meth
->finish
!= NULL
) && !dso
->meth
->finish(dso
)) {
84 DSOerr(DSO_F_DSO_FREE
, DSO_R_FINISH_FAILED
);
88 sk_void_free(dso
->meth_data
);
89 OPENSSL_free(dso
->filename
);
90 OPENSSL_free(dso
->loaded_filename
);
91 CRYPTO_THREAD_lock_free(dso
->lock
);
96 int DSO_flags(DSO
*dso
)
98 return ((dso
== NULL
) ? 0 : dso
->flags
);
101 int DSO_up_ref(DSO
*dso
)
106 DSOerr(DSO_F_DSO_UP_REF
, ERR_R_PASSED_NULL_PARAMETER
);
110 if (CRYPTO_UP_REF(&dso
->references
, &i
, dso
->lock
) <= 0)
113 REF_PRINT_COUNT("DSO", r
);
114 REF_ASSERT_ISNT(i
< 2);
115 return ((i
> 1) ? 1 : 0);
118 DSO
*DSO_load(DSO
*dso
, const char *filename
, DSO_METHOD
*meth
, int flags
)
124 ret
= DSO_new_method(meth
);
126 DSOerr(DSO_F_DSO_LOAD
, ERR_R_MALLOC_FAILURE
);
130 /* Pass the provided flags to the new DSO object */
131 if (DSO_ctrl(ret
, DSO_CTRL_SET_FLAGS
, flags
, NULL
) < 0) {
132 DSOerr(DSO_F_DSO_LOAD
, DSO_R_CTRL_FAILED
);
137 /* Don't load if we're currently already loaded */
138 if (ret
->filename
!= NULL
) {
139 DSOerr(DSO_F_DSO_LOAD
, DSO_R_DSO_ALREADY_LOADED
);
143 * filename can only be NULL if we were passed a dso that already has one
146 if (filename
!= NULL
)
147 if (!DSO_set_filename(ret
, filename
)) {
148 DSOerr(DSO_F_DSO_LOAD
, DSO_R_SET_FILENAME_FAILED
);
151 filename
= ret
->filename
;
152 if (filename
== NULL
) {
153 DSOerr(DSO_F_DSO_LOAD
, DSO_R_NO_FILENAME
);
156 if (ret
->meth
->dso_load
== NULL
) {
157 DSOerr(DSO_F_DSO_LOAD
, DSO_R_UNSUPPORTED
);
160 if (!ret
->meth
->dso_load(ret
)) {
161 DSOerr(DSO_F_DSO_LOAD
, DSO_R_LOAD_FAILED
);
172 DSO_FUNC_TYPE
DSO_bind_func(DSO
*dso
, const char *symname
)
174 DSO_FUNC_TYPE ret
= NULL
;
176 if ((dso
== NULL
) || (symname
== NULL
)) {
177 DSOerr(DSO_F_DSO_BIND_FUNC
, ERR_R_PASSED_NULL_PARAMETER
);
180 if (dso
->meth
->dso_bind_func
== NULL
) {
181 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_UNSUPPORTED
);
184 if ((ret
= dso
->meth
->dso_bind_func(dso
, symname
)) == NULL
) {
185 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_SYM_FAILURE
);
193 * I don't really like these *_ctrl functions very much to be perfectly
194 * honest. For one thing, I think I have to return a negative value for any
195 * error because possible DSO_ctrl() commands may return values such as
196 * "size"s that can legitimately be zero (making the standard
197 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
198 * times. I'd prefer "output" values to be passed by reference and the return
199 * value as success/failure like usual ... but we conform when we must... :-)
201 long DSO_ctrl(DSO
*dso
, int cmd
, long larg
, void *parg
)
204 DSOerr(DSO_F_DSO_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
208 * We should intercept certain generic commands and only pass control to
209 * the method-specific ctrl() function if it's something we don't handle.
212 case DSO_CTRL_GET_FLAGS
:
214 case DSO_CTRL_SET_FLAGS
:
215 dso
->flags
= (int)larg
;
217 case DSO_CTRL_OR_FLAGS
:
218 dso
->flags
|= (int)larg
;
223 if ((dso
->meth
== NULL
) || (dso
->meth
->dso_ctrl
== NULL
)) {
224 DSOerr(DSO_F_DSO_CTRL
, DSO_R_UNSUPPORTED
);
227 return (dso
->meth
->dso_ctrl(dso
, cmd
, larg
, parg
));
230 const char *DSO_get_filename(DSO
*dso
)
233 DSOerr(DSO_F_DSO_GET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
236 return (dso
->filename
);
239 int DSO_set_filename(DSO
*dso
, const char *filename
)
243 if ((dso
== NULL
) || (filename
== NULL
)) {
244 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
247 if (dso
->loaded_filename
) {
248 DSOerr(DSO_F_DSO_SET_FILENAME
, DSO_R_DSO_ALREADY_LOADED
);
251 /* We'll duplicate filename */
252 copied
= OPENSSL_strdup(filename
);
253 if (copied
== NULL
) {
254 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_MALLOC_FAILURE
);
257 OPENSSL_free(dso
->filename
);
258 dso
->filename
= copied
;
262 char *DSO_merge(DSO
*dso
, const char *filespec1
, const char *filespec2
)
266 if (dso
== NULL
|| filespec1
== NULL
) {
267 DSOerr(DSO_F_DSO_MERGE
, ERR_R_PASSED_NULL_PARAMETER
);
270 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
271 if (dso
->merger
!= NULL
)
272 result
= dso
->merger(dso
, filespec1
, filespec2
);
273 else if (dso
->meth
->dso_merger
!= NULL
)
274 result
= dso
->meth
->dso_merger(dso
, filespec1
, filespec2
);
279 char *DSO_convert_filename(DSO
*dso
, const char *filename
)
284 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
287 if (filename
== NULL
)
288 filename
= dso
->filename
;
289 if (filename
== NULL
) {
290 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, DSO_R_NO_FILENAME
);
293 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
294 if (dso
->name_converter
!= NULL
)
295 result
= dso
->name_converter(dso
, filename
);
296 else if (dso
->meth
->dso_name_converter
!= NULL
)
297 result
= dso
->meth
->dso_name_converter(dso
, filename
);
299 if (result
== NULL
) {
300 result
= OPENSSL_strdup(filename
);
301 if (result
== NULL
) {
302 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_MALLOC_FAILURE
);
309 int DSO_pathbyaddr(void *addr
, char *path
, int sz
)
311 DSO_METHOD
*meth
= default_DSO_meth
;
313 meth
= DSO_METHOD_openssl();
314 if (meth
->pathbyaddr
== NULL
) {
315 DSOerr(DSO_F_DSO_PATHBYADDR
, DSO_R_UNSUPPORTED
);
318 return (*meth
->pathbyaddr
) (addr
, path
, sz
);
321 DSO
*DSO_dsobyaddr(void *addr
, int flags
)
324 char *filename
= NULL
;
325 int len
= DSO_pathbyaddr(addr
, NULL
, 0);
327 filename
= OPENSSL_malloc(len
);
329 && DSO_pathbyaddr(addr
, filename
, len
) == len
)
330 ret
= DSO_load(NULL
, filename
, NULL
, flags
);
332 OPENSSL_free(filename
);
336 void *DSO_global_lookup(const char *name
)
338 DSO_METHOD
*meth
= default_DSO_meth
;
340 meth
= DSO_METHOD_openssl();
341 if (meth
->globallookup
== NULL
) {
342 DSOerr(DSO_F_DSO_GLOBAL_LOOKUP
, DSO_R_UNSUPPORTED
);
345 return (*meth
->globallookup
) (name
);