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