diff options
Diffstat (limited to 'src/lib/libcrypto/dso/dso_dlfcn.c')
-rw-r--r-- | src/lib/libcrypto/dso/dso_dlfcn.c | 325 |
1 files changed, 0 insertions, 325 deletions
diff --git a/src/lib/libcrypto/dso/dso_dlfcn.c b/src/lib/libcrypto/dso/dso_dlfcn.c deleted file mode 100644 index d48b4202f2..0000000000 --- a/src/lib/libcrypto/dso/dso_dlfcn.c +++ /dev/null | |||
@@ -1,325 +0,0 @@ | |||
1 | /* dso_dlfcn.c */ | ||
2 | /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL | ||
3 | * project 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 | #ifdef __linux | ||
60 | #define _GNU_SOURCE | ||
61 | #endif | ||
62 | |||
63 | #include <stdio.h> | ||
64 | #include "cryptlib.h" | ||
65 | #include <openssl/dso.h> | ||
66 | |||
67 | #ifndef DSO_DLFCN | ||
68 | DSO_METHOD *DSO_METHOD_dlfcn(void) | ||
69 | { | ||
70 | return NULL; | ||
71 | } | ||
72 | #else | ||
73 | |||
74 | #ifdef HAVE_DLFCN_H | ||
75 | #include <dlfcn.h> | ||
76 | #endif | ||
77 | |||
78 | /* Part of the hack in "dlfcn_load" ... */ | ||
79 | #define DSO_MAX_TRANSLATED_SIZE 256 | ||
80 | |||
81 | static int dlfcn_load(DSO *dso); | ||
82 | static int dlfcn_unload(DSO *dso); | ||
83 | static void *dlfcn_bind_var(DSO *dso, const char *symname); | ||
84 | static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname); | ||
85 | #if 0 | ||
86 | static int dlfcn_unbind(DSO *dso, char *symname, void *symptr); | ||
87 | static int dlfcn_init(DSO *dso); | ||
88 | static int dlfcn_finish(DSO *dso); | ||
89 | static long dlfcn_ctrl(DSO *dso, int cmd, long larg, void *parg); | ||
90 | #endif | ||
91 | static char *dlfcn_name_converter(DSO *dso, const char *filename); | ||
92 | |||
93 | static DSO_METHOD dso_meth_dlfcn = { | ||
94 | "OpenSSL 'dlfcn' shared library method", | ||
95 | dlfcn_load, | ||
96 | dlfcn_unload, | ||
97 | dlfcn_bind_var, | ||
98 | dlfcn_bind_func, | ||
99 | /* For now, "unbind" doesn't exist */ | ||
100 | #if 0 | ||
101 | NULL, /* unbind_var */ | ||
102 | NULL, /* unbind_func */ | ||
103 | #endif | ||
104 | NULL, /* ctrl */ | ||
105 | dlfcn_name_converter, | ||
106 | NULL, /* init */ | ||
107 | NULL /* finish */ | ||
108 | }; | ||
109 | |||
110 | DSO_METHOD *DSO_METHOD_dlfcn(void) | ||
111 | { | ||
112 | return(&dso_meth_dlfcn); | ||
113 | } | ||
114 | |||
115 | /* Prior to using the dlopen() function, we should decide on the flag | ||
116 | * we send. There's a few different ways of doing this and it's a | ||
117 | * messy venn-diagram to match up which platforms support what. So | ||
118 | * as we don't have autoconf yet, I'm implementing a hack that could | ||
119 | * be hacked further relatively easily to deal with cases as we find | ||
120 | * them. Initially this is to cope with OpenBSD. */ | ||
121 | #if defined(__OpenBSD__) || defined(__NetBSD__) | ||
122 | # ifdef DL_LAZY | ||
123 | # define DLOPEN_FLAG DL_LAZY | ||
124 | # else | ||
125 | # ifdef RTLD_NOW | ||
126 | # define DLOPEN_FLAG RTLD_NOW | ||
127 | # else | ||
128 | # define DLOPEN_FLAG 0 | ||
129 | # endif | ||
130 | # endif | ||
131 | #else | ||
132 | # ifdef OPENSSL_SYS_SUNOS | ||
133 | # define DLOPEN_FLAG 1 | ||
134 | # else | ||
135 | # define DLOPEN_FLAG RTLD_NOW /* Hope this works everywhere else */ | ||
136 | # endif | ||
137 | #endif | ||
138 | |||
139 | /* For this DSO_METHOD, our meth_data STACK will contain; | ||
140 | * (i) the handle (void*) returned from dlopen(). | ||
141 | */ | ||
142 | |||
143 | static int dlfcn_load(DSO *dso) | ||
144 | { | ||
145 | void *ptr = NULL; | ||
146 | /* See applicable comments in dso_dl.c */ | ||
147 | char *filename = DSO_convert_filename(dso, NULL); | ||
148 | |||
149 | if(filename == NULL) | ||
150 | { | ||
151 | DSOerr(DSO_F_DLFCN_LOAD,DSO_R_NO_FILENAME); | ||
152 | goto err; | ||
153 | } | ||
154 | ptr = dlopen(filename, DLOPEN_FLAG); | ||
155 | if(ptr == NULL) | ||
156 | { | ||
157 | DSOerr(DSO_F_DLFCN_LOAD,DSO_R_LOAD_FAILED); | ||
158 | ERR_add_error_data(4, "filename(", filename, "): ", dlerror()); | ||
159 | goto err; | ||
160 | } | ||
161 | if(!sk_push(dso->meth_data, (char *)ptr)) | ||
162 | { | ||
163 | DSOerr(DSO_F_DLFCN_LOAD,DSO_R_STACK_ERROR); | ||
164 | goto err; | ||
165 | } | ||
166 | /* Success */ | ||
167 | dso->loaded_filename = filename; | ||
168 | return(1); | ||
169 | err: | ||
170 | /* Cleanup! */ | ||
171 | if(filename != NULL) | ||
172 | OPENSSL_free(filename); | ||
173 | if(ptr != NULL) | ||
174 | dlclose(ptr); | ||
175 | return(0); | ||
176 | } | ||
177 | |||
178 | static int dlfcn_unload(DSO *dso) | ||
179 | { | ||
180 | void *ptr; | ||
181 | if(dso == NULL) | ||
182 | { | ||
183 | DSOerr(DSO_F_DLFCN_UNLOAD,ERR_R_PASSED_NULL_PARAMETER); | ||
184 | return(0); | ||
185 | } | ||
186 | if(sk_num(dso->meth_data) < 1) | ||
187 | return(1); | ||
188 | ptr = (void *)sk_pop(dso->meth_data); | ||
189 | if(ptr == NULL) | ||
190 | { | ||
191 | DSOerr(DSO_F_DLFCN_UNLOAD,DSO_R_NULL_HANDLE); | ||
192 | /* Should push the value back onto the stack in | ||
193 | * case of a retry. */ | ||
194 | sk_push(dso->meth_data, (char *)ptr); | ||
195 | return(0); | ||
196 | } | ||
197 | /* For now I'm not aware of any errors associated with dlclose() */ | ||
198 | dlclose(ptr); | ||
199 | return(1); | ||
200 | } | ||
201 | |||
202 | static void *dlfcn_bind_var(DSO *dso, const char *symname) | ||
203 | { | ||
204 | void *ptr, *sym; | ||
205 | |||
206 | if((dso == NULL) || (symname == NULL)) | ||
207 | { | ||
208 | DSOerr(DSO_F_DLFCN_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER); | ||
209 | return(NULL); | ||
210 | } | ||
211 | if(sk_num(dso->meth_data) < 1) | ||
212 | { | ||
213 | DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_STACK_ERROR); | ||
214 | return(NULL); | ||
215 | } | ||
216 | ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1); | ||
217 | if(ptr == NULL) | ||
218 | { | ||
219 | DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_NULL_HANDLE); | ||
220 | return(NULL); | ||
221 | } | ||
222 | sym = dlsym(ptr, symname); | ||
223 | if(sym == NULL) | ||
224 | { | ||
225 | DSOerr(DSO_F_DLFCN_BIND_VAR,DSO_R_SYM_FAILURE); | ||
226 | ERR_add_error_data(4, "symname(", symname, "): ", dlerror()); | ||
227 | return(NULL); | ||
228 | } | ||
229 | return(sym); | ||
230 | } | ||
231 | |||
232 | static DSO_FUNC_TYPE dlfcn_bind_func(DSO *dso, const char *symname) | ||
233 | { | ||
234 | void *ptr; | ||
235 | DSO_FUNC_TYPE sym, *tsym = &sym; | ||
236 | |||
237 | if((dso == NULL) || (symname == NULL)) | ||
238 | { | ||
239 | DSOerr(DSO_F_DLFCN_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER); | ||
240 | return(NULL); | ||
241 | } | ||
242 | if(sk_num(dso->meth_data) < 1) | ||
243 | { | ||
244 | DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_STACK_ERROR); | ||
245 | return(NULL); | ||
246 | } | ||
247 | ptr = (void *)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1); | ||
248 | if(ptr == NULL) | ||
249 | { | ||
250 | DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_NULL_HANDLE); | ||
251 | return(NULL); | ||
252 | } | ||
253 | *(void**)(tsym) = dlsym(ptr, symname); | ||
254 | if(sym == NULL) | ||
255 | { | ||
256 | DSOerr(DSO_F_DLFCN_BIND_FUNC,DSO_R_SYM_FAILURE); | ||
257 | ERR_add_error_data(4, "symname(", symname, "): ", dlerror()); | ||
258 | return(NULL); | ||
259 | } | ||
260 | return(sym); | ||
261 | } | ||
262 | |||
263 | static char *dlfcn_name_converter(DSO *dso, const char *filename) | ||
264 | { | ||
265 | char *translated; | ||
266 | int len, rsize, transform; | ||
267 | |||
268 | len = strlen(filename); | ||
269 | rsize = len + 1; | ||
270 | transform = (strstr(filename, "/") == NULL); | ||
271 | if(transform) | ||
272 | { | ||
273 | /* We will convert this to "%s.so" or "lib%s.so" */ | ||
274 | rsize += 3; /* The length of ".so" */ | ||
275 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) | ||
276 | rsize += 3; /* The length of "lib" */ | ||
277 | } | ||
278 | translated = OPENSSL_malloc(rsize); | ||
279 | if(translated == NULL) | ||
280 | { | ||
281 | DSOerr(DSO_F_DLFCN_NAME_CONVERTER, | ||
282 | DSO_R_NAME_TRANSLATION_FAILED); | ||
283 | return(NULL); | ||
284 | } | ||
285 | if(transform) | ||
286 | { | ||
287 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) | ||
288 | snprintf(translated, rsize, "lib%s.so", filename); | ||
289 | else | ||
290 | snprintf(translated, rsize, "%s.so", filename); | ||
291 | } | ||
292 | else | ||
293 | snprintf(translated, rsize, "%s", filename); | ||
294 | return(translated); | ||
295 | } | ||
296 | |||
297 | #ifdef OPENSSL_FIPS | ||
298 | static void dlfcn_ref_point(){} | ||
299 | |||
300 | int DSO_pathbyaddr(void *addr,char *path,int sz) | ||
301 | { | ||
302 | Dl_info dli; | ||
303 | int len; | ||
304 | |||
305 | if (addr == NULL) | ||
306 | { | ||
307 | union { void(*f)(void); void *p; } t = { dlfcn_ref_point }; | ||
308 | addr = t.p; | ||
309 | } | ||
310 | |||
311 | if (dladdr(addr,&dli)) | ||
312 | { | ||
313 | len = (int)strlen(dli.dli_fname); | ||
314 | if (sz <= 0) return len+1; | ||
315 | if (len >= sz) len=sz-1; | ||
316 | memcpy(path,dli.dli_fname,len); | ||
317 | path[len++]=0; | ||
318 | return len; | ||
319 | } | ||
320 | |||
321 | ERR_add_error_data(4, "dlfcn_pathbyaddr(): ", dlerror()); | ||
322 | return -1; | ||
323 | } | ||
324 | #endif | ||
325 | #endif /* DSO_DLFCN */ | ||