Convert CRYPTO_LOCK_DSO to new multi-threading API
[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
124     ret->lock = CRYPTO_THREAD_lock_new();
125     if (ret->lock == NULL) {
126         sk_void_free(ret->meth_data);
127         OPENSSL_free(ret);
128         return NULL;
129     }
130
131     if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
132         DSO_free(ret);
133         ret = NULL;
134     }
135
136     return ret;
137 }
138
139 int DSO_free(DSO *dso)
140 {
141     int i;
142
143     if (dso == NULL)
144         return (1);
145
146     if (CRYPTO_atomic_add(&dso->references, -1, &i, dso->lock) <= 0)
147         return 0;
148
149     REF_PRINT_COUNT("DSO", dso);
150     if (i > 0)
151         return 1;
152     REF_ASSERT_ISNT(i < 0);
153
154     if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
155         DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
156         return 0;
157     }
158
159     if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
160         DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
161         return 0;
162     }
163
164     sk_void_free(dso->meth_data);
165     OPENSSL_free(dso->filename);
166     OPENSSL_free(dso->loaded_filename);
167     CRYPTO_THREAD_lock_free(dso->lock);
168     OPENSSL_free(dso);
169     return 1;
170 }
171
172 int DSO_flags(DSO *dso)
173 {
174     return ((dso == NULL) ? 0 : dso->flags);
175 }
176
177 int DSO_up_ref(DSO *dso)
178 {
179     int i;
180
181     if (dso == NULL) {
182         DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
183         return 0;
184     }
185
186     if (CRYPTO_atomic_add(&dso->references, 1, &i, dso->lock) <= 0)
187         return 0;
188
189     REF_PRINT_COUNT("DSO", r);
190     REF_ASSERT_ISNT(i < 2);
191     return ((i > 1) ? 1 : 0);
192 }
193
194 DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
195 {
196     DSO *ret;
197     int allocated = 0;
198
199     if (dso == NULL) {
200         ret = DSO_new_method(meth);
201         if (ret == NULL) {
202             DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
203             goto err;
204         }
205         allocated = 1;
206         /* Pass the provided flags to the new DSO object */
207         if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
208             DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
209             goto err;
210         }
211     } else
212         ret = dso;
213     /* Don't load if we're currently already loaded */
214     if (ret->filename != NULL) {
215         DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
216         goto err;
217     }
218     /*
219      * filename can only be NULL if we were passed a dso that already has one
220      * set.
221      */
222     if (filename != NULL)
223         if (!DSO_set_filename(ret, filename)) {
224             DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
225             goto err;
226         }
227     filename = ret->filename;
228     if (filename == NULL) {
229         DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
230         goto err;
231     }
232     if (ret->meth->dso_load == NULL) {
233         DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
234         goto err;
235     }
236     if (!ret->meth->dso_load(ret)) {
237         DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
238         goto err;
239     }
240     /* Load succeeded */
241     return (ret);
242  err:
243     if (allocated)
244         DSO_free(ret);
245     return (NULL);
246 }
247
248 void *DSO_bind_var(DSO *dso, const char *symname)
249 {
250     void *ret = NULL;
251
252     if ((dso == NULL) || (symname == NULL)) {
253         DSOerr(DSO_F_DSO_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
254         return (NULL);
255     }
256     if (dso->meth->dso_bind_var == NULL) {
257         DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_UNSUPPORTED);
258         return (NULL);
259     }
260     if ((ret = dso->meth->dso_bind_var(dso, symname)) == NULL) {
261         DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_SYM_FAILURE);
262         return (NULL);
263     }
264     /* Success */
265     return (ret);
266 }
267
268 DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
269 {
270     DSO_FUNC_TYPE ret = NULL;
271
272     if ((dso == NULL) || (symname == NULL)) {
273         DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
274         return (NULL);
275     }
276     if (dso->meth->dso_bind_func == NULL) {
277         DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
278         return (NULL);
279     }
280     if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
281         DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
282         return (NULL);
283     }
284     /* Success */
285     return (ret);
286 }
287
288 /*
289  * I don't really like these *_ctrl functions very much to be perfectly
290  * honest. For one thing, I think I have to return a negative value for any
291  * error because possible DSO_ctrl() commands may return values such as
292  * "size"s that can legitimately be zero (making the standard
293  * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
294  * times. I'd prefer "output" values to be passed by reference and the return
295  * value as success/failure like usual ... but we conform when we must... :-)
296  */
297 long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
298 {
299     if (dso == NULL) {
300         DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
301         return (-1);
302     }
303     /*
304      * We should intercept certain generic commands and only pass control to
305      * the method-specific ctrl() function if it's something we don't handle.
306      */
307     switch (cmd) {
308     case DSO_CTRL_GET_FLAGS:
309         return dso->flags;
310     case DSO_CTRL_SET_FLAGS:
311         dso->flags = (int)larg;
312         return (0);
313     case DSO_CTRL_OR_FLAGS:
314         dso->flags |= (int)larg;
315         return (0);
316     default:
317         break;
318     }
319     if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
320         DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
321         return (-1);
322     }
323     return (dso->meth->dso_ctrl(dso, cmd, larg, parg));
324 }
325
326 int DSO_set_name_converter(DSO *dso, DSO_NAME_CONVERTER_FUNC cb,
327                            DSO_NAME_CONVERTER_FUNC *oldcb)
328 {
329     if (dso == NULL) {
330         DSOerr(DSO_F_DSO_SET_NAME_CONVERTER, ERR_R_PASSED_NULL_PARAMETER);
331         return (0);
332     }
333     if (oldcb)
334         *oldcb = dso->name_converter;
335     dso->name_converter = cb;
336     return (1);
337 }
338
339 const char *DSO_get_filename(DSO *dso)
340 {
341     if (dso == NULL) {
342         DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
343         return (NULL);
344     }
345     return (dso->filename);
346 }
347
348 int DSO_set_filename(DSO *dso, const char *filename)
349 {
350     char *copied;
351
352     if ((dso == NULL) || (filename == NULL)) {
353         DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
354         return (0);
355     }
356     if (dso->loaded_filename) {
357         DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
358         return (0);
359     }
360     /* We'll duplicate filename */
361     copied = OPENSSL_strdup(filename);
362     if (copied == NULL) {
363         DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
364         return (0);
365     }
366     OPENSSL_free(dso->filename);
367     dso->filename = copied;
368     return (1);
369 }
370
371 char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
372 {
373     char *result = NULL;
374
375     if (dso == NULL || filespec1 == NULL) {
376         DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
377         return (NULL);
378     }
379     if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
380         if (dso->merger != NULL)
381             result = dso->merger(dso, filespec1, filespec2);
382         else if (dso->meth->dso_merger != NULL)
383             result = dso->meth->dso_merger(dso, filespec1, filespec2);
384     }
385     return (result);
386 }
387
388 char *DSO_convert_filename(DSO *dso, const char *filename)
389 {
390     char *result = NULL;
391
392     if (dso == NULL) {
393         DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
394         return (NULL);
395     }
396     if (filename == NULL)
397         filename = dso->filename;
398     if (filename == NULL) {
399         DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
400         return (NULL);
401     }
402     if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
403         if (dso->name_converter != NULL)
404             result = dso->name_converter(dso, filename);
405         else if (dso->meth->dso_name_converter != NULL)
406             result = dso->meth->dso_name_converter(dso, filename);
407     }
408     if (result == NULL) {
409         result = OPENSSL_strdup(filename);
410         if (result == NULL) {
411             DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
412             return (NULL);
413         }
414     }
415     return (result);
416 }
417
418 const char *DSO_get_loaded_filename(DSO *dso)
419 {
420     if (dso == NULL) {
421         DSOerr(DSO_F_DSO_GET_LOADED_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
422         return (NULL);
423     }
424     return (dso->loaded_filename);
425 }
426
427 int DSO_pathbyaddr(void *addr, char *path, int sz)
428 {
429     DSO_METHOD *meth = default_DSO_meth;
430     if (meth == NULL)
431         meth = DSO_METHOD_openssl();
432     if (meth->pathbyaddr == NULL) {
433         DSOerr(DSO_F_DSO_PATHBYADDR, DSO_R_UNSUPPORTED);
434         return -1;
435     }
436     return (*meth->pathbyaddr) (addr, path, sz);
437 }
438
439 void *DSO_global_lookup(const char *name)
440 {
441     DSO_METHOD *meth = default_DSO_meth;
442     if (meth == NULL)
443         meth = DSO_METHOD_openssl();
444     if (meth->globallookup == NULL) {
445         DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
446         return NULL;
447     }
448     return (*meth->globallookup) (name);
449 }