1 /* dso_lib.c -*- mode:C; c-file-style: "eay" -*- */
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/crypto.h>
62 #include "internal/cryptlib.h"
63 #include <openssl/dso.h>
65 static DSO_METHOD
*default_DSO_meth
= NULL
;
69 return (DSO_new_method(NULL
));
72 void DSO_set_default_method(DSO_METHOD
*meth
)
74 default_DSO_meth
= meth
;
77 DSO_METHOD
*DSO_get_default_method(void)
79 return (default_DSO_meth
);
82 DSO_METHOD
*DSO_get_method(DSO
*dso
)
87 DSO_METHOD
*DSO_set_method(DSO
*dso
, DSO_METHOD
*meth
)
95 DSO
*DSO_new_method(DSO_METHOD
*meth
)
99 if (default_DSO_meth
== NULL
) {
101 * We default to DSO_METH_openssl() which in turn defaults to
102 * stealing the "best available" method. Will fallback to
103 * DSO_METH_null() in the worst case.
105 default_DSO_meth
= DSO_METHOD_openssl();
107 ret
= OPENSSL_zalloc(sizeof(*ret
));
109 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
112 ret
->meth_data
= sk_void_new_null();
113 if (ret
->meth_data
== NULL
) {
114 /* sk_new doesn't generate any errors so we do */
115 DSOerr(DSO_F_DSO_NEW_METHOD
, ERR_R_MALLOC_FAILURE
);
120 ret
->meth
= default_DSO_meth
;
124 if ((ret
->meth
->init
!= NULL
) && !ret
->meth
->init(ret
)) {
131 int DSO_free(DSO
*dso
)
138 i
= CRYPTO_add(&dso
->references
, -1, CRYPTO_LOCK_DSO
);
140 REF_PRINT("DSO", dso
);
146 fprintf(stderr
, "DSO_free, bad reference count\n");
151 if ((dso
->meth
->dso_unload
!= NULL
) && !dso
->meth
->dso_unload(dso
)) {
152 DSOerr(DSO_F_DSO_FREE
, DSO_R_UNLOAD_FAILED
);
156 if ((dso
->meth
->finish
!= NULL
) && !dso
->meth
->finish(dso
)) {
157 DSOerr(DSO_F_DSO_FREE
, DSO_R_FINISH_FAILED
);
161 sk_void_free(dso
->meth_data
);
162 OPENSSL_free(dso
->filename
);
163 OPENSSL_free(dso
->loaded_filename
);
168 int DSO_flags(DSO
*dso
)
170 return ((dso
== NULL
) ? 0 : dso
->flags
);
173 int DSO_up_ref(DSO
*dso
)
176 DSOerr(DSO_F_DSO_UP_REF
, ERR_R_PASSED_NULL_PARAMETER
);
180 CRYPTO_add(&dso
->references
, 1, CRYPTO_LOCK_DSO
);
184 DSO
*DSO_load(DSO
*dso
, const char *filename
, DSO_METHOD
*meth
, int flags
)
190 ret
= DSO_new_method(meth
);
192 DSOerr(DSO_F_DSO_LOAD
, ERR_R_MALLOC_FAILURE
);
196 /* Pass the provided flags to the new DSO object */
197 if (DSO_ctrl(ret
, DSO_CTRL_SET_FLAGS
, flags
, NULL
) < 0) {
198 DSOerr(DSO_F_DSO_LOAD
, DSO_R_CTRL_FAILED
);
203 /* Don't load if we're currently already loaded */
204 if (ret
->filename
!= NULL
) {
205 DSOerr(DSO_F_DSO_LOAD
, DSO_R_DSO_ALREADY_LOADED
);
209 * filename can only be NULL if we were passed a dso that already has one
212 if (filename
!= NULL
)
213 if (!DSO_set_filename(ret
, filename
)) {
214 DSOerr(DSO_F_DSO_LOAD
, DSO_R_SET_FILENAME_FAILED
);
217 filename
= ret
->filename
;
218 if (filename
== NULL
) {
219 DSOerr(DSO_F_DSO_LOAD
, DSO_R_NO_FILENAME
);
222 if (ret
->meth
->dso_load
== NULL
) {
223 DSOerr(DSO_F_DSO_LOAD
, DSO_R_UNSUPPORTED
);
226 if (!ret
->meth
->dso_load(ret
)) {
227 DSOerr(DSO_F_DSO_LOAD
, DSO_R_LOAD_FAILED
);
238 void *DSO_bind_var(DSO
*dso
, const char *symname
)
242 if ((dso
== NULL
) || (symname
== NULL
)) {
243 DSOerr(DSO_F_DSO_BIND_VAR
, ERR_R_PASSED_NULL_PARAMETER
);
246 if (dso
->meth
->dso_bind_var
== NULL
) {
247 DSOerr(DSO_F_DSO_BIND_VAR
, DSO_R_UNSUPPORTED
);
250 if ((ret
= dso
->meth
->dso_bind_var(dso
, symname
)) == NULL
) {
251 DSOerr(DSO_F_DSO_BIND_VAR
, DSO_R_SYM_FAILURE
);
258 DSO_FUNC_TYPE
DSO_bind_func(DSO
*dso
, const char *symname
)
260 DSO_FUNC_TYPE ret
= NULL
;
262 if ((dso
== NULL
) || (symname
== NULL
)) {
263 DSOerr(DSO_F_DSO_BIND_FUNC
, ERR_R_PASSED_NULL_PARAMETER
);
266 if (dso
->meth
->dso_bind_func
== NULL
) {
267 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_UNSUPPORTED
);
270 if ((ret
= dso
->meth
->dso_bind_func(dso
, symname
)) == NULL
) {
271 DSOerr(DSO_F_DSO_BIND_FUNC
, DSO_R_SYM_FAILURE
);
279 * I don't really like these *_ctrl functions very much to be perfectly
280 * honest. For one thing, I think I have to return a negative value for any
281 * error because possible DSO_ctrl() commands may return values such as
282 * "size"s that can legitimately be zero (making the standard
283 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
284 * times. I'd prefer "output" values to be passed by reference and the return
285 * value as success/failure like usual ... but we conform when we must... :-)
287 long DSO_ctrl(DSO
*dso
, int cmd
, long larg
, void *parg
)
290 DSOerr(DSO_F_DSO_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
294 * We should intercept certain generic commands and only pass control to
295 * the method-specific ctrl() function if it's something we don't handle.
298 case DSO_CTRL_GET_FLAGS
:
300 case DSO_CTRL_SET_FLAGS
:
301 dso
->flags
= (int)larg
;
303 case DSO_CTRL_OR_FLAGS
:
304 dso
->flags
|= (int)larg
;
309 if ((dso
->meth
== NULL
) || (dso
->meth
->dso_ctrl
== NULL
)) {
310 DSOerr(DSO_F_DSO_CTRL
, DSO_R_UNSUPPORTED
);
313 return (dso
->meth
->dso_ctrl(dso
, cmd
, larg
, parg
));
316 int DSO_set_name_converter(DSO
*dso
, DSO_NAME_CONVERTER_FUNC cb
,
317 DSO_NAME_CONVERTER_FUNC
*oldcb
)
320 DSOerr(DSO_F_DSO_SET_NAME_CONVERTER
, ERR_R_PASSED_NULL_PARAMETER
);
324 *oldcb
= dso
->name_converter
;
325 dso
->name_converter
= cb
;
329 const char *DSO_get_filename(DSO
*dso
)
332 DSOerr(DSO_F_DSO_GET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
335 return (dso
->filename
);
338 int DSO_set_filename(DSO
*dso
, const char *filename
)
342 if ((dso
== NULL
) || (filename
== NULL
)) {
343 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
346 if (dso
->loaded_filename
) {
347 DSOerr(DSO_F_DSO_SET_FILENAME
, DSO_R_DSO_ALREADY_LOADED
);
350 /* We'll duplicate filename */
351 copied
= OPENSSL_malloc(strlen(filename
) + 1);
352 if (copied
== NULL
) {
353 DSOerr(DSO_F_DSO_SET_FILENAME
, ERR_R_MALLOC_FAILURE
);
356 OPENSSL_strlcpy(copied
, filename
, strlen(filename
) + 1);
357 OPENSSL_free(dso
->filename
);
358 dso
->filename
= copied
;
362 char *DSO_merge(DSO
*dso
, const char *filespec1
, const char *filespec2
)
366 if (dso
== NULL
|| filespec1
== NULL
) {
367 DSOerr(DSO_F_DSO_MERGE
, ERR_R_PASSED_NULL_PARAMETER
);
370 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
371 if (dso
->merger
!= NULL
)
372 result
= dso
->merger(dso
, filespec1
, filespec2
);
373 else if (dso
->meth
->dso_merger
!= NULL
)
374 result
= dso
->meth
->dso_merger(dso
, filespec1
, filespec2
);
379 char *DSO_convert_filename(DSO
*dso
, const char *filename
)
384 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
387 if (filename
== NULL
)
388 filename
= dso
->filename
;
389 if (filename
== NULL
) {
390 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, DSO_R_NO_FILENAME
);
393 if ((dso
->flags
& DSO_FLAG_NO_NAME_TRANSLATION
) == 0) {
394 if (dso
->name_converter
!= NULL
)
395 result
= dso
->name_converter(dso
, filename
);
396 else if (dso
->meth
->dso_name_converter
!= NULL
)
397 result
= dso
->meth
->dso_name_converter(dso
, filename
);
399 if (result
== NULL
) {
400 result
= OPENSSL_malloc(strlen(filename
) + 1);
401 if (result
== NULL
) {
402 DSOerr(DSO_F_DSO_CONVERT_FILENAME
, ERR_R_MALLOC_FAILURE
);
405 OPENSSL_strlcpy(result
, filename
, strlen(filename
) + 1);
410 const char *DSO_get_loaded_filename(DSO
*dso
)
413 DSOerr(DSO_F_DSO_GET_LOADED_FILENAME
, ERR_R_PASSED_NULL_PARAMETER
);
416 return (dso
->loaded_filename
);
419 int DSO_pathbyaddr(void *addr
, char *path
, int sz
)
421 DSO_METHOD
*meth
= default_DSO_meth
;
423 meth
= DSO_METHOD_openssl();
424 if (meth
->pathbyaddr
== NULL
) {
425 DSOerr(DSO_F_DSO_PATHBYADDR
, DSO_R_UNSUPPORTED
);
428 return (*meth
->pathbyaddr
) (addr
, path
, sz
);
431 void *DSO_global_lookup(const char *name
)
433 DSO_METHOD
*meth
= default_DSO_meth
;
435 meth
= DSO_METHOD_openssl();
436 if (meth
->globallookup
== NULL
) {
437 DSOerr(DSO_F_DSO_GLOBAL_LOOKUP
, DSO_R_UNSUPPORTED
);
440 return (*meth
->globallookup
) (name
);