]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dso/dso_lib.c
RT3495: Add a hash for faster dup detection.
[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
59#include <stdio.h>
60#include <openssl/crypto.h>
b39fc560 61#include "internal/cryptlib.h"
8f4fac7f
GT
62#include <openssl/dso.h>
63
64static DSO_METHOD *default_DSO_meth = NULL;
65
66DSO *DSO_new(void)
0f113f3e
MC
67{
68 return (DSO_new_method(NULL));
69}
8f4fac7f
GT
70
71void DSO_set_default_method(DSO_METHOD *meth)
0f113f3e
MC
72{
73 default_DSO_meth = meth;
74}
8f4fac7f
GT
75
76DSO_METHOD *DSO_get_default_method(void)
0f113f3e
MC
77{
78 return (default_DSO_meth);
79}
8f4fac7f
GT
80
81DSO_METHOD *DSO_get_method(DSO *dso)
0f113f3e
MC
82{
83 return (dso->meth);
84}
8f4fac7f
GT
85
86DSO_METHOD *DSO_set_method(DSO *dso, DSO_METHOD *meth)
0f113f3e
MC
87{
88 DSO_METHOD *mtmp;
89 mtmp = dso->meth;
90 dso->meth = meth;
91 return (mtmp);
92}
8f4fac7f
GT
93
94DSO *DSO_new_method(DSO_METHOD *meth)
0f113f3e
MC
95{
96 DSO *ret;
97
b196e7d9 98 if (default_DSO_meth == NULL) {
0f113f3e
MC
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();
b196e7d9 105 }
b51bce94 106 ret = OPENSSL_zalloc(sizeof(*ret));
0f113f3e
MC
107 if (ret == NULL) {
108 DSOerr(DSO_F_DSO_NEW_METHOD, ERR_R_MALLOC_FAILURE);
109 return (NULL);
110 }
0f113f3e
MC
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)) {
98637bd3 124 sk_void_free(ret->meth_data);
0f113f3e
MC
125 OPENSSL_free(ret);
126 ret = NULL;
127 }
128 return (ret);
129}
8f4fac7f
GT
130
131int DSO_free(DSO *dso)
0f113f3e
MC
132{
133 int i;
134
efa7dd64
RS
135 if (dso == NULL)
136 return (1);
0f113f3e
MC
137
138 i = CRYPTO_add(&dso->references, -1, CRYPTO_LOCK_DSO);
8f4fac7f 139#ifdef REF_PRINT
0f113f3e 140 REF_PRINT("DSO", dso);
8f4fac7f 141#endif
0f113f3e
MC
142 if (i > 0)
143 return (1);
8f4fac7f 144#ifdef REF_CHECK
0f113f3e
MC
145 if (i < 0) {
146 fprintf(stderr, "DSO_free, bad reference count\n");
147 abort();
148 }
8f4fac7f
GT
149#endif
150
0f113f3e
MC
151 if ((dso->meth->dso_unload != NULL) && !dso->meth->dso_unload(dso)) {
152 DSOerr(DSO_F_DSO_FREE, DSO_R_UNLOAD_FAILED);
153 return (0);
154 }
8f4fac7f 155
0f113f3e
MC
156 if ((dso->meth->finish != NULL) && !dso->meth->finish(dso)) {
157 DSOerr(DSO_F_DSO_FREE, DSO_R_FINISH_FAILED);
158 return (0);
159 }
160
161 sk_void_free(dso->meth_data);
b548a1f1
RS
162 OPENSSL_free(dso->filename);
163 OPENSSL_free(dso->loaded_filename);
0f113f3e
MC
164 OPENSSL_free(dso);
165 return (1);
166}
167
168int DSO_flags(DSO *dso)
169{
170 return ((dso == NULL) ? 0 : dso->flags);
171}
8f4fac7f 172
d9ff8890 173int DSO_up_ref(DSO *dso)
0f113f3e
MC
174{
175 if (dso == NULL) {
176 DSOerr(DSO_F_DSO_UP_REF, ERR_R_PASSED_NULL_PARAMETER);
177 return (0);
178 }
8f4fac7f 179
0f113f3e
MC
180 CRYPTO_add(&dso->references, 1, CRYPTO_LOCK_DSO);
181 return (1);
182}
8f4fac7f 183
b9e63915 184DSO *DSO_load(DSO *dso, const char *filename, DSO_METHOD *meth, int flags)
0f113f3e
MC
185{
186 DSO *ret;
187 int allocated = 0;
188
189 if (dso == NULL) {
190 ret = DSO_new_method(meth);
191 if (ret == NULL) {
192 DSOerr(DSO_F_DSO_LOAD, ERR_R_MALLOC_FAILURE);
193 goto err;
194 }
195 allocated = 1;
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);
199 goto err;
200 }
201 } else
202 ret = dso;
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);
206 goto err;
207 }
208 /*
209 * filename can only be NULL if we were passed a dso that already has one
210 * set.
211 */
212 if (filename != NULL)
213 if (!DSO_set_filename(ret, filename)) {
214 DSOerr(DSO_F_DSO_LOAD, DSO_R_SET_FILENAME_FAILED);
215 goto err;
216 }
217 filename = ret->filename;
218 if (filename == NULL) {
219 DSOerr(DSO_F_DSO_LOAD, DSO_R_NO_FILENAME);
220 goto err;
221 }
222 if (ret->meth->dso_load == NULL) {
223 DSOerr(DSO_F_DSO_LOAD, DSO_R_UNSUPPORTED);
224 goto err;
225 }
226 if (!ret->meth->dso_load(ret)) {
227 DSOerr(DSO_F_DSO_LOAD, DSO_R_LOAD_FAILED);
228 goto err;
229 }
230 /* Load succeeded */
231 return (ret);
232 err:
233 if (allocated)
234 DSO_free(ret);
235 return (NULL);
236}
8f4fac7f 237
e9a68cfb 238void *DSO_bind_var(DSO *dso, const char *symname)
0f113f3e
MC
239{
240 void *ret = NULL;
241
242 if ((dso == NULL) || (symname == NULL)) {
243 DSOerr(DSO_F_DSO_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
244 return (NULL);
245 }
246 if (dso->meth->dso_bind_var == NULL) {
247 DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_UNSUPPORTED);
248 return (NULL);
249 }
250 if ((ret = dso->meth->dso_bind_var(dso, symname)) == NULL) {
251 DSOerr(DSO_F_DSO_BIND_VAR, DSO_R_SYM_FAILURE);
252 return (NULL);
253 }
254 /* Success */
255 return (ret);
256}
e9a68cfb
GT
257
258DSO_FUNC_TYPE DSO_bind_func(DSO *dso, const char *symname)
0f113f3e
MC
259{
260 DSO_FUNC_TYPE ret = NULL;
261
262 if ((dso == NULL) || (symname == NULL)) {
263 DSOerr(DSO_F_DSO_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
264 return (NULL);
265 }
266 if (dso->meth->dso_bind_func == NULL) {
267 DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_UNSUPPORTED);
268 return (NULL);
269 }
270 if ((ret = dso->meth->dso_bind_func(dso, symname)) == NULL) {
271 DSOerr(DSO_F_DSO_BIND_FUNC, DSO_R_SYM_FAILURE);
272 return (NULL);
273 }
274 /* Success */
275 return (ret);
276}
277
278/*
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
61986d32 283 * "if (DSO_cmd(...))" form that works almost everywhere else fail at odd
0f113f3e
MC
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... :-)
286 */
b9e63915 287long DSO_ctrl(DSO *dso, int cmd, long larg, void *parg)
0f113f3e
MC
288{
289 if (dso == NULL) {
290 DSOerr(DSO_F_DSO_CTRL, ERR_R_PASSED_NULL_PARAMETER);
291 return (-1);
292 }
293 /*
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.
296 */
297 switch (cmd) {
298 case DSO_CTRL_GET_FLAGS:
299 return dso->flags;
300 case DSO_CTRL_SET_FLAGS:
301 dso->flags = (int)larg;
302 return (0);
303 case DSO_CTRL_OR_FLAGS:
304 dso->flags |= (int)larg;
305 return (0);
306 default:
307 break;
308 }
309 if ((dso->meth == NULL) || (dso->meth->dso_ctrl == NULL)) {
310 DSOerr(DSO_F_DSO_CTRL, DSO_R_UNSUPPORTED);
311 return (-1);
312 }
313 return (dso->meth->dso_ctrl(dso, cmd, larg, parg));
314}
51c8dc37
GT
315
316int DSO_set_name_converter(DSO *dso, DSO_NAME_CONVERTER_FUNC cb,
0f113f3e
MC
317 DSO_NAME_CONVERTER_FUNC *oldcb)
318{
319 if (dso == NULL) {
320 DSOerr(DSO_F_DSO_SET_NAME_CONVERTER, ERR_R_PASSED_NULL_PARAMETER);
321 return (0);
322 }
323 if (oldcb)
324 *oldcb = dso->name_converter;
325 dso->name_converter = cb;
326 return (1);
327}
51c8dc37
GT
328
329const char *DSO_get_filename(DSO *dso)
0f113f3e
MC
330{
331 if (dso == NULL) {
332 DSOerr(DSO_F_DSO_GET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
333 return (NULL);
334 }
335 return (dso->filename);
336}
51c8dc37
GT
337
338int DSO_set_filename(DSO *dso, const char *filename)
0f113f3e
MC
339{
340 char *copied;
341
342 if ((dso == NULL) || (filename == NULL)) {
343 DSOerr(DSO_F_DSO_SET_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
344 return (0);
345 }
346 if (dso->loaded_filename) {
347 DSOerr(DSO_F_DSO_SET_FILENAME, DSO_R_DSO_ALREADY_LOADED);
348 return (0);
349 }
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);
354 return (0);
355 }
7644a9ae 356 OPENSSL_strlcpy(copied, filename, strlen(filename) + 1);
b548a1f1 357 OPENSSL_free(dso->filename);
0f113f3e
MC
358 dso->filename = copied;
359 return (1);
360}
51c8dc37 361
cbecb3ac 362char *DSO_merge(DSO *dso, const char *filespec1, const char *filespec2)
0f113f3e
MC
363{
364 char *result = NULL;
365
366 if (dso == NULL || filespec1 == NULL) {
367 DSOerr(DSO_F_DSO_MERGE, ERR_R_PASSED_NULL_PARAMETER);
368 return (NULL);
369 }
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);
375 }
376 return (result);
377}
cbecb3ac 378
51c8dc37 379char *DSO_convert_filename(DSO *dso, const char *filename)
0f113f3e
MC
380{
381 char *result = NULL;
382
383 if (dso == NULL) {
384 DSOerr(DSO_F_DSO_CONVERT_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
385 return (NULL);
386 }
387 if (filename == NULL)
388 filename = dso->filename;
389 if (filename == NULL) {
390 DSOerr(DSO_F_DSO_CONVERT_FILENAME, DSO_R_NO_FILENAME);
391 return (NULL);
392 }
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);
398 }
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);
403 return (NULL);
404 }
7644a9ae 405 OPENSSL_strlcpy(result, filename, strlen(filename) + 1);
0f113f3e
MC
406 }
407 return (result);
408}
51c8dc37
GT
409
410const char *DSO_get_loaded_filename(DSO *dso)
0f113f3e
MC
411{
412 if (dso == NULL) {
413 DSOerr(DSO_F_DSO_GET_LOADED_FILENAME, ERR_R_PASSED_NULL_PARAMETER);
414 return (NULL);
415 }
416 return (dso->loaded_filename);
417}
418
419int DSO_pathbyaddr(void *addr, char *path, int sz)
420{
421 DSO_METHOD *meth = default_DSO_meth;
422 if (meth == NULL)
423 meth = DSO_METHOD_openssl();
424 if (meth->pathbyaddr == NULL) {
425 DSOerr(DSO_F_DSO_PATHBYADDR, DSO_R_UNSUPPORTED);
426 return -1;
427 }
428 return (*meth->pathbyaddr) (addr, path, sz);
429}
68b64fb6 430
c6cb42e4 431void *DSO_global_lookup(const char *name)
0f113f3e
MC
432{
433 DSO_METHOD *meth = default_DSO_meth;
434 if (meth == NULL)
435 meth = DSO_METHOD_openssl();
436 if (meth->globallookup == NULL) {
437 DSOerr(DSO_F_DSO_GLOBAL_LOOKUP, DSO_R_UNSUPPORTED);
438 return NULL;
439 }
440 return (*meth->globallookup) (name);
441}