Remove outdated DEBUG flags.
[openssl.git] / crypto / dso / dso_lib.c
1 /*
2  * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
3  * 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #include <stdio.h>
60 #include <openssl/crypto.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/dso.h>
63
64 static DSO_METHOD *default_DSO_meth = NULL;
65
66 DSO *DSO_new(void)
67 {
68     return (DSO_new_method(NULL));
69 }
70
71 void DSO_set_default_method(DSO_METHOD *meth)
72 {
73     default_DSO_meth = meth;
74 }
75
76 DSO_METHOD *DSO_get_default_method(void)
77 {
78     return (default_DSO_meth);
79 }
80
81 DSO_METHOD *DSO_get_method(DSO *dso)
82 {
83     return (dso->meth);
84 }
85
86 DSO_METHOD *DSO_set_method(DSO *dso, DSO_METHOD *meth)
87 {
88     DSO_METHOD *mtmp;
89     mtmp = dso->meth;
90     dso->meth = meth;
91     return (mtmp);
92 }
93
94 DSO *DSO_new_method(DSO_METHOD *meth)
95 {
96     DSO *ret;
97
98     if (default_DSO_meth == NULL) {
99         /*
100          * We default to DSO_METH_openssl() which in turn defaults to
101          * stealing the "best available" method. Will fallback to
102          * DSO_METH_null() in the worst case.
103          */
104         default_DSO_meth = DSO_METHOD_openssl();
105     }
106     ret = OPENSSL_zalloc(sizeof(*ret));
107     if (ret == NULL) {
108         DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
109         return (NULL);
110     }
111     ret->meth_data = sk_void_new_null();
112     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);
115         OPENSSL_free(ret);
116         return (NULL);
117     }
118     if (meth == NULL)
119         ret->meth = default_DSO_meth;
120     else
121         ret->meth = meth;
122     ret->references = 1;
123     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
124         sk_void_free(ret->meth_data);
125         OPENSSL_free(ret);
126         ret = NULL;
127     }
128     return (ret);
129 }
130
131 int DSO_free(DSO *dso)
132 {
133     int i;
134
135     if (dso == NULL)
136         return (1);
137
138     i = CRYPTO_add(&dso->references, -1, CRYPTO_LOCK_DSO);
139     REF_PRINT_COUNT("DSO", dso);
140     if (i > 0)
141         return (1);
142     REF_ASSERT_ISNT(i < 0);
143
144     if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
145         DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
146         return (0);
147     }
148
149     if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
150         DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
151         return (0);
152     }
153
154     sk_void_free(dso->meth_data);
155     OPENSSL_free(dso->filename);
156     OPENSSL_free(dso->loaded_filename);
157     OPENSSL_free(dso);
158     return (1);
159 }
160
161 int DSO_flags(DSO *dso)
162 {
163     return ((dso == NULL) ? 0 : dso->flags);
164 }
165
166 int DSO_up_ref(DSO *dso)
167 {
168     if (dso == NULL) {
169         DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
170         return (0);
171     }
172
173     CRYPTO_add(&dso->references, 1, CRYPTO_LOCK_DSO);
174     return (1);
175 }
176
177 DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
178 {
179     DSO *ret;
180     int allocated = 0;
181
182     if (dso == NULL) {
183         ret = DSO_new_method(meth);
184         if (ret == NULL) {
185             DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
186             goto err;
187         }
188         allocated = 1;
189         /* Pass the provided flags to the new DSO object */
190         if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
191             DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
192             goto err;
193         }
194     } else
195         ret = dso;
196     /* Don't load if we're currently already loaded */
197     if (ret->filename != NULL) {
198         DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
199         goto err;
200     }
201     /*
202      * filename can only be NULL if we were passed a dso that already has one
203      * set.
204      */
205     if (filename != NULL)
206         if (!DSO_set_filename(ret, filename)) {
207             DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
208             goto err;
209         }
210     filename = ret->filename;
211     if (filename == NULL) {
212         DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
213         goto err;
214     }
215     if (ret->meth->dso_load == NULL) {
216         DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
217         goto err;
218     }
219     if (!ret->meth->dso_load(ret)) {
220         DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
221         goto err;
222     }
223     /* Load succeeded */
224     return (ret);
225  err:
226     if (allocated)
227         DSO_free(ret);
228     return (NULL);
229 }
230
231 void *DSO_bind_var(DSO *dso, const char *symname)
232 {
233     void *ret = NULL;
234
235     if ((dso == NULL) || (symname == NULL)) {
236         DSOerr(DSO_F_DSO_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
237         return (NULL);
238     }
239     if (dso->meth->dso_bind_var == NULL) {
240         DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_UNSUPPORTED);
241         return (NULL);
242     }
243     if ((ret = dso->meth->dso_bind_var(dso, symname)) == NULL) {
244         DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_SYM_FAILURE);
245         return (NULL);
246     }
247     /* Success */
248     return (ret);
249 }
250
251 DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
252 {
253     DSO_FUNC_TYPE ret = NULL;
254
255     if ((dso == NULL) || (symname == NULL)) {
256         DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
257         return (NULL);
258     }
259     if (dso->meth->dso_bind_func == NULL) {
260         DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
261         return (NULL);
262     }
263     if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
264         DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
265         return (NULL);
266     }
267     /* Success */
268     return (ret);
269 }
270
271 /*
272  * I don't really like these *_ctrl functions very much to be perfectly
273  * honest. For one thing, I think I have to return a negative value for any
274  * error because possible DSO_ctrl() commands may return values such as
275  * "size"s that can legitimately be zero (making the standard
276  * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
277  * times. I'd prefer "output" values to be passed by reference and the return
278  * value as success/failure like usual ... but we conform when we must... :-)
279  */
280 long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
281 {
282     if (dso == NULL) {
283         DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
284         return (-1);
285     }
286     /*
287      * We should intercept certain generic commands and only pass control to
288      * the method-specific ctrl() function if it's something we don't handle.
289      */
290     switch (cmd) {
291     case DSO_CTRL_GET_FLAGS:
292         return dso->flags;
293     case DSO_CTRL_SET_FLAGS:
294         dso->flags = (int)larg;
295         return (0);
296     case DSO_CTRL_OR_FLAGS:
297         dso->flags |= (int)larg;
298         return (0);
299     default:
300         break;
301     }
302     if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
303         DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
304         return (-1);
305     }
306     return (dso->meth->dso_ctrl(dso, cmd, larg, parg));
307 }
308
309 int DSO_set_name_converter(DSO *dso, DSO_NAME_CONVERTER_FUNC cb,
310                            DSO_NAME_CONVERTER_FUNC *oldcb)
311 {
312     if (dso == NULL) {
313         DSOerr(DSO_F_DSO_SET_NAME_CONVERTER, ERR_R_PASSED_NULL_PARAMETER);
314         return (0);
315     }
316     if (oldcb)
317         *oldcb = dso->name_converter;
318     dso->name_converter = cb;
319     return (1);
320 }
321
322 const char *DSO_get_filename(DSO *dso)
323 {
324     if (dso == NULL) {
325         DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
326         return (NULL);
327     }
328     return (dso->filename);
329 }
330
331 int DSO_set_filename(DSO *dso, const char *filename)
332 {
333     char *copied;
334
335     if ((dso == NULL) || (filename == NULL)) {
336         DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
337         return (0);
338     }
339     if (dso->loaded_filename) {
340         DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
341         return (0);
342     }
343     /* We'll duplicate filename */
344     copied = OPENSSL_malloc(strlen(filename) + 1);
345     if (copied == NULL) {
346         DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
347         return (0);
348     }
349     OPENSSL_strlcpy(copied, filename, strlen(filename) + 1);
350     OPENSSL_free(dso->filename);
351     dso->filename = copied;
352     return (1);
353 }
354
355 char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
356 {
357     char *result = NULL;
358
359     if (dso == NULL || filespec1 == NULL) {
360         DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
361         return (NULL);
362     }
363     if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
364         if (dso->merger != NULL)
365             result = dso->merger(dso, filespec1, filespec2);
366         else if (dso->meth->dso_merger != NULL)
367             result = dso->meth->dso_merger(dso, filespec1, filespec2);
368     }
369     return (result);
370 }
371
372 char *DSO_convert_filename(DSO *dso, const char *filename)
373 {
374     char *result = NULL;
375
376     if (dso == NULL) {
377         DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
378         return (NULL);
379     }
380     if (filename == NULL)
381         filename = dso->filename;
382     if (filename == NULL) {
383         DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
384         return (NULL);
385     }
386     if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
387         if (dso->name_converter != NULL)
388             result = dso->name_converter(dso, filename);
389         else if (dso->meth->dso_name_converter != NULL)
390             result = dso->meth->dso_name_converter(dso, filename);
391     }
392     if (result == NULL) {
393         result = OPENSSL_malloc(strlen(filename) + 1);
394         if (result == NULL) {
395             DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
396             return (NULL);
397         }
398         OPENSSL_strlcpy(result, filename, strlen(filename) + 1);
399     }
400     return (result);
401 }
402
403 const char *DSO_get_loaded_filename(DSO *dso)
404 {
405     if (dso == NULL) {
406         DSOerr(DSO_F_DSO_GET_LOADED_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
407         return (NULL);
408     }
409     return (dso->loaded_filename);
410 }
411
412 int DSO_pathbyaddr(void *addr, char *path, int sz)
413 {
414     DSO_METHOD *meth = default_DSO_meth;
415     if (meth == NULL)
416         meth = DSO_METHOD_openssl();
417     if (meth->pathbyaddr == NULL) {
418         DSOerr(DSO_F_DSO_PATHBYADDR, DSO_R_UNSUPPORTED);
419         return -1;
420     }
421     return (*meth->pathbyaddr) (addr, path, sz);
422 }
423
424 void *DSO_global_lookup(const char *name)
425 {
426     DSO_METHOD *meth = default_DSO_meth;
427     if (meth == NULL)
428         meth = DSO_METHOD_openssl();
429     if (meth->globallookup == NULL) {
430         DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
431         return NULL;
432     }
433     return (*meth->globallookup) (name);
434 }