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