2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/crypto.h>
62 #include <openssl/dso.h>
64 static DSO_METHOD *default_DSO_meth = NULL;
68 return(DSO_new_method(NULL));
71 void DSO_set_default_method(DSO_METHOD *meth)
73 default_DSO_meth = meth;
76 DSO_METHOD *DSO_get_default_method(void)
78 return(default_DSO_meth);
81 DSO_METHOD *DSO_get_method(DSO *dso)
86 DSO_METHOD *DSO_set_method(DSO *dso, DSO_METHOD *meth)
94 DSO *DSO_new_method(DSO_METHOD *meth)
98 if(default_DSO_meth == NULL)
99 /* We default to DSO_METH_openssl() which in turn defaults
100 * to stealing the "best available" method. Will fallback
101 * to DSO_METH_null() in the worst case. */
102 default_DSO_meth = DSO_METHOD_openssl();
103 ret = (DSO *)OPENSSL_malloc(sizeof(DSO));
106 DSOerr(DSO_F_DSO_NEW_METHOD,ERR_R_MALLOC_FAILURE);
109 memset(ret, 0, sizeof(DSO));
110 ret->meth_data = sk_new_null();
111 if(ret->meth_data == NULL)
113 /* sk_new doesn't generate any errors so we do */
114 DSOerr(DSO_F_DSO_NEW_METHOD,ERR_R_MALLOC_FAILURE);
119 ret->meth = default_DSO_meth;
123 if((ret->meth->init != NULL) && !ret->meth->init(ret))
131 int DSO_free(DSO *dso)
137 DSOerr(DSO_F_DSO_FREE,ERR_R_PASSED_NULL_PARAMETER);
141 i=CRYPTO_add(&dso->references,-1,CRYPTO_LOCK_DSO);
143 REF_PRINT("DSO",dso);
149 fprintf(stderr,"DSO_free, bad reference count\n");
154 if((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso))
156 DSOerr(DSO_F_DSO_FREE,DSO_R_UNLOAD_FAILED);
160 if((dso->meth->finish != NULL) && !dso->meth->finish(dso))
162 DSOerr(DSO_F_DSO_FREE,DSO_R_FINISH_FAILED);
166 sk_free(dso->meth_data);
167 if(dso->filename != NULL)
168 OPENSSL_free(dso->filename);
169 if(dso->loaded_filename != NULL)
170 OPENSSL_free(dso->loaded_filename);
176 int DSO_flags(DSO *dso)
178 return((dso == NULL) ? 0 : dso->flags);
186 DSOerr(DSO_F_DSO_UP,ERR_R_PASSED_NULL_PARAMETER);
190 CRYPTO_add(&dso->references,1,CRYPTO_LOCK_DSO);
194 DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
201 ret = DSO_new_method(meth);
204 DSOerr(DSO_F_DSO_LOAD,ERR_R_MALLOC_FAILURE);
211 /* Don't load if we're currently already loaded */
212 if(ret->filename != NULL)
214 DSOerr(DSO_F_DSO_LOAD,DSO_R_DSO_ALREADY_LOADED);
217 /* filename can only be NULL if we were passed a dso that already has
220 if(!DSO_set_filename(ret, filename))
222 DSOerr(DSO_F_DSO_LOAD,DSO_R_SET_FILENAME_FAILED);
225 filename = ret->filename;
228 DSOerr(DSO_F_DSO_LOAD,DSO_R_NO_FILENAME);
231 /* Bleurgh ... have to check for negative return values for
232 * errors. <grimace> */
233 if(DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0)
235 DSOerr(DSO_F_DSO_LOAD,DSO_R_CTRL_FAILED);
238 if(ret->meth->dso_load == NULL)
240 DSOerr(DSO_F_DSO_LOAD,DSO_R_UNSUPPORTED);
243 if(!ret->meth->dso_load(ret))
245 DSOerr(DSO_F_DSO_LOAD,DSO_R_LOAD_FAILED);
256 void *DSO_bind_var(DSO *dso, const char *symname)
260 if((dso == NULL) || (symname == NULL))
262 DSOerr(DSO_F_DSO_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER);
265 if(dso->meth->dso_bind_var == NULL)
267 DSOerr(DSO_F_DSO_BIND_VAR,DSO_R_UNSUPPORTED);
270 if((ret = dso->meth->dso_bind_var(dso, symname)) == NULL)
272 DSOerr(DSO_F_DSO_BIND_VAR,DSO_R_SYM_FAILURE);
279 DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
281 DSO_FUNC_TYPE ret = NULL;
283 if((dso == NULL) || (symname == NULL))
285 DSOerr(DSO_F_DSO_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER);
288 if(dso->meth->dso_bind_func == NULL)
290 DSOerr(DSO_F_DSO_BIND_FUNC,DSO_R_UNSUPPORTED);
293 if((ret = dso->meth->dso_bind_func(dso, symname)) == NULL)
295 DSOerr(DSO_F_DSO_BIND_FUNC,DSO_R_SYM_FAILURE);
302 /* I don't really like these *_ctrl functions very much to be perfectly
303 * honest. For one thing, I think I have to return a negative value for
304 * any error because possible DSO_ctrl() commands may return values
305 * such as "size"s that can legitimately be zero (making the standard
306 * "if(DSO_cmd(...))" form that works almost everywhere else fail at
307 * odd times. I'd prefer "output" values to be passed by reference and
308 * the return value as success/failure like usual ... but we conform
309 * when we must... :-) */
310 long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
314 DSOerr(DSO_F_DSO_CTRL,ERR_R_PASSED_NULL_PARAMETER);
317 /* We should intercept certain generic commands and only pass control
318 * to the method-specific ctrl() function if it's something we don't
322 case DSO_CTRL_GET_FLAGS:
324 case DSO_CTRL_SET_FLAGS:
325 dso->flags = (int)larg;
327 case DSO_CTRL_OR_FLAGS:
328 dso->flags |= (int)larg;
333 if((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL))
335 DSOerr(DSO_F_DSO_CTRL,DSO_R_UNSUPPORTED);
338 return(dso->meth->dso_ctrl(dso,cmd,larg,parg));
341 int DSO_set_name_converter(DSO *dso, DSO_NAME_CONVERTER_FUNC cb,
342 DSO_NAME_CONVERTER_FUNC *oldcb)
346 DSOerr(DSO_F_DSO_SET_NAME_CONVERTER,
347 ERR_R_PASSED_NULL_PARAMETER);
351 *oldcb = dso->name_converter;
352 dso->name_converter = cb;
356 const char *DSO_get_filename(DSO *dso)
360 DSOerr(DSO_F_DSO_GET_FILENAME,ERR_R_PASSED_NULL_PARAMETER);
363 return(dso->filename);
366 int DSO_set_filename(DSO *dso, const char *filename)
370 if((dso == NULL) || (filename == NULL))
372 DSOerr(DSO_F_DSO_SET_FILENAME,ERR_R_PASSED_NULL_PARAMETER);
375 if(dso->loaded_filename)
377 DSOerr(DSO_F_DSO_SET_FILENAME,DSO_R_DSO_ALREADY_LOADED);
380 /* We'll duplicate filename */
381 copied = OPENSSL_malloc(strlen(filename) + 1);
384 DSOerr(DSO_F_DSO_SET_FILENAME,ERR_R_MALLOC_FAILURE);
387 strcpy(copied, filename);
389 OPENSSL_free(dso->filename);
390 dso->filename = copied;
394 char *DSO_convert_filename(DSO *dso, const char *filename)
400 DSOerr(DSO_F_DSO_CONVERT_FILENAME,ERR_R_PASSED_NULL_PARAMETER);
404 filename = dso->filename;
407 DSOerr(DSO_F_DSO_CONVERT_FILENAME,DSO_R_NO_FILENAME);
410 if((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0)
412 if(dso->name_converter != NULL)
413 result = dso->name_converter(dso, filename);
414 else if(dso->meth->dso_name_converter != NULL)
415 result = dso->meth->dso_name_converter(dso, filename);
419 result = OPENSSL_malloc(strlen(filename) + 1);
422 DSOerr(DSO_F_DSO_CONVERT_FILENAME,
423 ERR_R_MALLOC_FAILURE);
426 strcpy(result, filename);
431 const char *DSO_get_loaded_filename(DSO *dso)
435 DSOerr(DSO_F_DSO_GET_LOADED_FILENAME,
436 ERR_R_PASSED_NULL_PARAMETER);
439 return(dso->loaded_filename);