]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dso/dso_lib.c
Copyright consolidation 06/10
[thirdparty/openssl.git] / crypto / dso / dso_lib.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
3 * 2000.
8f4fac7f
GT
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
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
8f4fac7f
GT
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
73decf59 59#include "dso_locl.h"
8f4fac7f
GT
60
61static DSO_METHOD *default_DSO_meth = NULL;
62
3d8b2ec4 63static DSO *DSO_new_method(DSO_METHOD *meth)
0f113f3e
MC
64{
65 DSO *ret;
66
b196e7d9 67 if (default_DSO_meth == NULL) {
0f113f3e
MC
68 /*
69 * We default to DSO_METH_openssl() which in turn defaults to
70 * stealing the "best available" method. Will fallback to
71 * DSO_METH_null() in the worst case.
72 */
73 default_DSO_meth = DSO_METHOD_openssl();
b196e7d9 74 }
b51bce94 75 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e
MC
76 if (ret == NULL) {
77 DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
78 return (NULL);
79 }
0f113f3e
MC
80 ret->meth_data = sk_void_new_null();
81 if (ret->meth_data == NULL) {
82 /* sk_new doesn't generate any errors so we do */
83 DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
84 OPENSSL_free(ret);
85 return (NULL);
86 }
3d8b2ec4 87 ret->meth = default_DSO_meth;
0f113f3e 88 ret->references = 1;
c74471d2
AG
89 ret->lock = CRYPTO_THREAD_lock_new();
90 if (ret->lock == NULL) {
98637bd3 91 sk_void_free(ret->meth_data);
0f113f3e 92 OPENSSL_free(ret);
c74471d2
AG
93 return NULL;
94 }
95
96 if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
97 DSO_free(ret);
0f113f3e
MC
98 ret = NULL;
99 }
c74471d2
AG
100
101 return ret;
0f113f3e 102}
8f4fac7f 103
3d8b2ec4
RS
104DSO *DSO_new(void)
105{
106 return DSO_new_method(NULL);
107}
108
8f4fac7f 109int DSO_free(DSO *dso)
0f113f3e
MC
110{
111 int i;
112
efa7dd64
RS
113 if (dso == NULL)
114 return (1);
0f113f3e 115
c74471d2
AG
116 if (CRYPTO_atomic_add(&dso->references, -1, &i, dso->lock) <= 0)
117 return 0;
118
f3f1cf84 119 REF_PRINT_COUNT("DSO", dso);
0f113f3e 120 if (i > 0)
c74471d2 121 return 1;
f3f1cf84 122 REF_ASSERT_ISNT(i < 0);
8f4fac7f 123
0f113f3e
MC
124 if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
125 DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
c74471d2 126 return 0;
0f113f3e 127 }
8f4fac7f 128
0f113f3e
MC
129 if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
130 DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
c74471d2 131 return 0;
0f113f3e
MC
132 }
133
134 sk_void_free(dso->meth_data);
b548a1f1
RS
135 OPENSSL_free(dso->filename);
136 OPENSSL_free(dso->loaded_filename);
c74471d2 137 CRYPTO_THREAD_lock_free(dso->lock);
0f113f3e 138 OPENSSL_free(dso);
c74471d2 139 return 1;
0f113f3e
MC
140}
141
142int DSO_flags(DSO *dso)
143{
144 return ((dso == NULL) ? 0 : dso->flags);
145}
8f4fac7f 146
d9ff8890 147int DSO_up_ref(DSO *dso)
0f113f3e 148{
c74471d2
AG
149 int i;
150
0f113f3e
MC
151 if (dso == NULL) {
152 DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
c74471d2 153 return 0;
0f113f3e 154 }
8f4fac7f 155
c74471d2
AG
156 if (CRYPTO_atomic_add(&dso->references, 1, &i, dso->lock) <= 0)
157 return 0;
158
159 REF_PRINT_COUNT("DSO", r);
160 REF_ASSERT_ISNT(i < 2);
161 return ((i > 1) ? 1 : 0);
0f113f3e 162}
8f4fac7f 163
b9e63915 164DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
0f113f3e
MC
165{
166 DSO *ret;
167 int allocated = 0;
168
169 if (dso == NULL) {
170 ret = DSO_new_method(meth);
171 if (ret == NULL) {
172 DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
173 goto err;
174 }
175 allocated = 1;
176 /* Pass the provided flags to the new DSO object */
177 if (DSO_ctrl(ret, DSO_CTRL_SET_FLAGS, flags, NULL) < 0) {
178 DSOerr(DSO_F_DSO_LOAD, DSO_R_CTRL_FAILED);
179 goto err;
180 }
181 } else
182 ret = dso;
183 /* Don't load if we're currently already loaded */
184 if (ret->filename != NULL) {
185 DSOerr(DSO_F_DSO_LOAD, DSO_R_DSO_ALREADY_LOADED);
186 goto err;
187 }
188 /*
189 * filename can only be NULL if we were passed a dso that already has one
190 * set.
191 */
192 if (filename != NULL)
193 if (!DSO_set_filename(ret, filename)) {
194 DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
195 goto err;
196 }
197 filename = ret->filename;
198 if (filename == NULL) {
199 DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
200 goto err;
201 }
202 if (ret->meth->dso_load == NULL) {
203 DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
204 goto err;
205 }
206 if (!ret->meth->dso_load(ret)) {
207 DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
208 goto err;
209 }
210 /* Load succeeded */
211 return (ret);
212 err:
213 if (allocated)
214 DSO_free(ret);
215 return (NULL);
216}
8f4fac7f 217
e9a68cfb 218DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
0f113f3e
MC
219{
220 DSO_FUNC_TYPE ret = NULL;
221
222 if ((dso == NULL) || (symname == NULL)) {
223 DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
224 return (NULL);
225 }
226 if (dso->meth->dso_bind_func == NULL) {
227 DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
228 return (NULL);
229 }
230 if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
231 DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
232 return (NULL);
233 }
234 /* Success */
235 return (ret);
236}
237
238/*
239 * I don't really like these *_ctrl functions very much to be perfectly
240 * honest. For one thing, I think I have to return a negative value for any
241 * error because possible DSO_ctrl() commands may return values such as
242 * "size"s that can legitimately be zero (making the standard
61986d32 243 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
0f113f3e
MC
244 * times. I'd prefer "output" values to be passed by reference and the return
245 * value as success/failure like usual ... but we conform when we must... :-)
246 */
b9e63915 247long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
0f113f3e
MC
248{
249 if (dso == NULL) {
250 DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
251 return (-1);
252 }
253 /*
254 * We should intercept certain generic commands and only pass control to
255 * the method-specific ctrl() function if it's something we don't handle.
256 */
257 switch (cmd) {
258 case DSO_CTRL_GET_FLAGS:
259 return dso->flags;
260 case DSO_CTRL_SET_FLAGS:
261 dso->flags = (int)larg;
262 return (0);
263 case DSO_CTRL_OR_FLAGS:
264 dso->flags |= (int)larg;
265 return (0);
266 default:
267 break;
268 }
269 if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
270 DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
271 return (-1);
272 }
273 return (dso->meth->dso_ctrl(dso, cmd, larg, parg));
274}
51c8dc37 275
51c8dc37 276const char *DSO_get_filename(DSO *dso)
0f113f3e
MC
277{
278 if (dso == NULL) {
279 DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
280 return (NULL);
281 }
282 return (dso->filename);
283}
51c8dc37
GT
284
285int DSO_set_filename(DSO *dso, const char *filename)
0f113f3e
MC
286{
287 char *copied;
288
289 if ((dso == NULL) || (filename == NULL)) {
290 DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
291 return (0);
292 }
293 if (dso->loaded_filename) {
294 DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
295 return (0);
296 }
297 /* We'll duplicate filename */
edae9834 298 copied = OPENSSL_strdup(filename);
0f113f3e
MC
299 if (copied == NULL) {
300 DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_MALLOC_FAILURE);
301 return (0);
302 }
b548a1f1 303 OPENSSL_free(dso->filename);
0f113f3e
MC
304 dso->filename = copied;
305 return (1);
306}
51c8dc37 307
cbecb3ac 308char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
0f113f3e
MC
309{
310 char *result = NULL;
311
312 if (dso == NULL || filespec1 == NULL) {
313 DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
314 return (NULL);
315 }
316 if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
317 if (dso->merger != NULL)
318 result = dso->merger(dso, filespec1, filespec2);
319 else if (dso->meth->dso_merger != NULL)
320 result = dso->meth->dso_merger(dso, filespec1, filespec2);
321 }
322 return (result);
323}
cbecb3ac 324
51c8dc37 325char *DSO_convert_filename(DSO *dso, const char *filename)
0f113f3e
MC
326{
327 char *result = NULL;
328
329 if (dso == NULL) {
330 DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
331 return (NULL);
332 }
333 if (filename == NULL)
334 filename = dso->filename;
335 if (filename == NULL) {
336 DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
337 return (NULL);
338 }
339 if ((dso->flags & DSO_FLAG_NO_NAME_TRANSLATION) == 0) {
340 if (dso->name_converter != NULL)
341 result = dso->name_converter(dso, filename);
342 else if (dso->meth->dso_name_converter != NULL)
343 result = dso->meth->dso_name_converter(dso, filename);
344 }
345 if (result == NULL) {
edae9834 346 result = OPENSSL_strdup(filename);
0f113f3e
MC
347 if (result == NULL) {
348 DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_MALLOC_FAILURE);
349 return (NULL);
350 }
0f113f3e
MC
351 }
352 return (result);
353}
51c8dc37 354
c6cb42e4 355void *DSO_global_lookup(const char *name)
0f113f3e
MC
356{
357 DSO_METHOD *meth = default_DSO_meth;
358 if (meth == NULL)
359 meth = DSO_METHOD_openssl();
360 if (meth->globallookup == NULL) {
361 DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
362 return NULL;
363 }
364 return (*meth->globallookup) (name);
365}