diff options
Diffstat (limited to '')
-rw-r--r-- | src/lib/libcrypto/dso/dso_dl.c | 317 |
1 files changed, 317 insertions, 0 deletions
diff --git a/src/lib/libcrypto/dso/dso_dl.c b/src/lib/libcrypto/dso/dso_dl.c new file mode 100644 index 0000000000..f7b4dfc0c3 --- /dev/null +++ b/src/lib/libcrypto/dso/dso_dl.c | |||
@@ -0,0 +1,317 @@ | |||
1 | /* dso_dl.c */ | ||
2 | /* Written by Richard Levitte (richard@levitte.org) 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 | #include <stdio.h> | ||
60 | #include "cryptlib.h" | ||
61 | #include <openssl/dso.h> | ||
62 | |||
63 | #ifndef DSO_DL | ||
64 | DSO_METHOD *DSO_METHOD_dl(void) | ||
65 | { | ||
66 | return NULL; | ||
67 | } | ||
68 | #else | ||
69 | |||
70 | #include <dl.h> | ||
71 | |||
72 | /* Part of the hack in "dl_load" ... */ | ||
73 | #define DSO_MAX_TRANSLATED_SIZE 256 | ||
74 | |||
75 | static int dl_load(DSO *dso); | ||
76 | static int dl_unload(DSO *dso); | ||
77 | static void *dl_bind_var(DSO *dso, const char *symname); | ||
78 | static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname); | ||
79 | #if 0 | ||
80 | static int dl_unbind_var(DSO *dso, char *symname, void *symptr); | ||
81 | static int dl_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr); | ||
82 | static int dl_init(DSO *dso); | ||
83 | static int dl_finish(DSO *dso); | ||
84 | static int dl_ctrl(DSO *dso, int cmd, long larg, void *parg); | ||
85 | #endif | ||
86 | static char *dl_name_converter(DSO *dso, const char *filename); | ||
87 | |||
88 | static DSO_METHOD dso_meth_dl = { | ||
89 | "OpenSSL 'dl' shared library method", | ||
90 | dl_load, | ||
91 | dl_unload, | ||
92 | dl_bind_var, | ||
93 | dl_bind_func, | ||
94 | /* For now, "unbind" doesn't exist */ | ||
95 | #if 0 | ||
96 | NULL, /* unbind_var */ | ||
97 | NULL, /* unbind_func */ | ||
98 | #endif | ||
99 | NULL, /* ctrl */ | ||
100 | dl_name_converter, | ||
101 | NULL, /* init */ | ||
102 | NULL /* finish */ | ||
103 | }; | ||
104 | |||
105 | DSO_METHOD *DSO_METHOD_dl(void) | ||
106 | { | ||
107 | return(&dso_meth_dl); | ||
108 | } | ||
109 | |||
110 | /* For this DSO_METHOD, our meth_data STACK will contain; | ||
111 | * (i) the handle (shl_t) returned from shl_load(). | ||
112 | * NB: I checked on HPUX11 and shl_t is itself a pointer | ||
113 | * type so the cast is safe. | ||
114 | */ | ||
115 | |||
116 | static int dl_load(DSO *dso) | ||
117 | { | ||
118 | shl_t ptr = NULL; | ||
119 | /* We don't do any fancy retries or anything, just take the method's | ||
120 | * (or DSO's if it has the callback set) best translation of the | ||
121 | * platform-independant filename and try once with that. */ | ||
122 | char *filename= DSO_convert_filename(dso, NULL); | ||
123 | |||
124 | if(filename == NULL) | ||
125 | { | ||
126 | DSOerr(DSO_F_DL_LOAD,DSO_R_NO_FILENAME); | ||
127 | goto err; | ||
128 | } | ||
129 | ptr = shl_load(filename, BIND_IMMEDIATE | | ||
130 | (dso->flags&DSO_FLAG_NO_NAME_TRANSLATION?0:DYNAMIC_PATH), 0L); | ||
131 | if(ptr == NULL) | ||
132 | { | ||
133 | DSOerr(DSO_F_DL_LOAD,DSO_R_LOAD_FAILED); | ||
134 | ERR_add_error_data(4, "filename(", filename, "): ", | ||
135 | strerror(errno)); | ||
136 | goto err; | ||
137 | } | ||
138 | if(!sk_push(dso->meth_data, (char *)ptr)) | ||
139 | { | ||
140 | DSOerr(DSO_F_DL_LOAD,DSO_R_STACK_ERROR); | ||
141 | goto err; | ||
142 | } | ||
143 | /* Success, stick the converted filename we've loaded under into the DSO | ||
144 | * (it also serves as the indicator that we are currently loaded). */ | ||
145 | dso->loaded_filename = filename; | ||
146 | return(1); | ||
147 | err: | ||
148 | /* Cleanup! */ | ||
149 | if(filename != NULL) | ||
150 | OPENSSL_free(filename); | ||
151 | if(ptr != NULL) | ||
152 | shl_unload(ptr); | ||
153 | return(0); | ||
154 | } | ||
155 | |||
156 | static int dl_unload(DSO *dso) | ||
157 | { | ||
158 | shl_t ptr; | ||
159 | if(dso == NULL) | ||
160 | { | ||
161 | DSOerr(DSO_F_DL_UNLOAD,ERR_R_PASSED_NULL_PARAMETER); | ||
162 | return(0); | ||
163 | } | ||
164 | if(sk_num(dso->meth_data) < 1) | ||
165 | return(1); | ||
166 | /* Is this statement legal? */ | ||
167 | ptr = (shl_t)sk_pop(dso->meth_data); | ||
168 | if(ptr == NULL) | ||
169 | { | ||
170 | DSOerr(DSO_F_DL_UNLOAD,DSO_R_NULL_HANDLE); | ||
171 | /* Should push the value back onto the stack in | ||
172 | * case of a retry. */ | ||
173 | sk_push(dso->meth_data, (char *)ptr); | ||
174 | return(0); | ||
175 | } | ||
176 | shl_unload(ptr); | ||
177 | return(1); | ||
178 | } | ||
179 | |||
180 | static void *dl_bind_var(DSO *dso, const char *symname) | ||
181 | { | ||
182 | shl_t ptr; | ||
183 | void *sym; | ||
184 | |||
185 | if((dso == NULL) || (symname == NULL)) | ||
186 | { | ||
187 | DSOerr(DSO_F_DL_BIND_VAR,ERR_R_PASSED_NULL_PARAMETER); | ||
188 | return(NULL); | ||
189 | } | ||
190 | if(sk_num(dso->meth_data) < 1) | ||
191 | { | ||
192 | DSOerr(DSO_F_DL_BIND_VAR,DSO_R_STACK_ERROR); | ||
193 | return(NULL); | ||
194 | } | ||
195 | ptr = (shl_t)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1); | ||
196 | if(ptr == NULL) | ||
197 | { | ||
198 | DSOerr(DSO_F_DL_BIND_VAR,DSO_R_NULL_HANDLE); | ||
199 | return(NULL); | ||
200 | } | ||
201 | if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) | ||
202 | { | ||
203 | DSOerr(DSO_F_DL_BIND_VAR,DSO_R_SYM_FAILURE); | ||
204 | ERR_add_error_data(4, "symname(", symname, "): ", | ||
205 | strerror(errno)); | ||
206 | return(NULL); | ||
207 | } | ||
208 | return(sym); | ||
209 | } | ||
210 | |||
211 | static DSO_FUNC_TYPE dl_bind_func(DSO *dso, const char *symname) | ||
212 | { | ||
213 | shl_t ptr; | ||
214 | void *sym; | ||
215 | |||
216 | if((dso == NULL) || (symname == NULL)) | ||
217 | { | ||
218 | DSOerr(DSO_F_DL_BIND_FUNC,ERR_R_PASSED_NULL_PARAMETER); | ||
219 | return(NULL); | ||
220 | } | ||
221 | if(sk_num(dso->meth_data) < 1) | ||
222 | { | ||
223 | DSOerr(DSO_F_DL_BIND_FUNC,DSO_R_STACK_ERROR); | ||
224 | return(NULL); | ||
225 | } | ||
226 | ptr = (shl_t)sk_value(dso->meth_data, sk_num(dso->meth_data) - 1); | ||
227 | if(ptr == NULL) | ||
228 | { | ||
229 | DSOerr(DSO_F_DL_BIND_FUNC,DSO_R_NULL_HANDLE); | ||
230 | return(NULL); | ||
231 | } | ||
232 | if (shl_findsym(&ptr, symname, TYPE_UNDEFINED, &sym) < 0) | ||
233 | { | ||
234 | DSOerr(DSO_F_DL_BIND_FUNC,DSO_R_SYM_FAILURE); | ||
235 | ERR_add_error_data(4, "symname(", symname, "): ", | ||
236 | strerror(errno)); | ||
237 | return(NULL); | ||
238 | } | ||
239 | return((DSO_FUNC_TYPE)sym); | ||
240 | } | ||
241 | |||
242 | /* This function is identical to the one in dso_dlfcn.c, but as it is highly | ||
243 | * unlikely that both the "dl" *and* "dlfcn" variants are being compiled at the | ||
244 | * same time, there's no great duplicating the code. Figuring out an elegant | ||
245 | * way to share one copy of the code would be more difficult and would not | ||
246 | * leave the implementations independant. */ | ||
247 | #if defined(__hpux) | ||
248 | static const char extension[] = ".sl"; | ||
249 | #else | ||
250 | static const char extension[] = ".so"; | ||
251 | #endif | ||
252 | static char *dl_name_converter(DSO *dso, const char *filename) | ||
253 | { | ||
254 | char *translated; | ||
255 | int len, rsize, transform; | ||
256 | |||
257 | len = strlen(filename); | ||
258 | rsize = len + 1; | ||
259 | transform = (strstr(filename, "/") == NULL); | ||
260 | { | ||
261 | /* We will convert this to "%s.s?" or "lib%s.s?" */ | ||
262 | rsize += strlen(extension);/* The length of ".s?" */ | ||
263 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) | ||
264 | rsize += 3; /* The length of "lib" */ | ||
265 | } | ||
266 | translated = OPENSSL_malloc(rsize); | ||
267 | if(translated == NULL) | ||
268 | { | ||
269 | DSOerr(DSO_F_DL_NAME_CONVERTER, | ||
270 | DSO_R_NAME_TRANSLATION_FAILED); | ||
271 | return(NULL); | ||
272 | } | ||
273 | if(transform) | ||
274 | { | ||
275 | if ((DSO_flags(dso) & DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == 0) | ||
276 | sprintf(translated, "lib%s%s", filename, extension); | ||
277 | else | ||
278 | sprintf(translated, "%s%s", filename, extension); | ||
279 | } | ||
280 | else | ||
281 | sprintf(translated, "%s", filename); | ||
282 | return(translated); | ||
283 | } | ||
284 | |||
285 | #ifdef OPENSSL_FIPS | ||
286 | static void dl_ref_point(){} | ||
287 | |||
288 | int DSO_pathbyaddr(void *addr,char *path,int sz) | ||
289 | { | ||
290 | struct shl_descriptor inf; | ||
291 | int i,len; | ||
292 | |||
293 | if (addr == NULL) | ||
294 | { | ||
295 | union { void(*f)(); void *p; } t = { dl_ref_point }; | ||
296 | addr = t.p; | ||
297 | } | ||
298 | |||
299 | for (i=-1;shl_get_r(i,&inf)==0;i++) | ||
300 | { | ||
301 | if (((size_t)addr >= inf.tstart && (size_t)addr < inf.tend) || | ||
302 | ((size_t)addr >= inf.dstart && (size_t)addr < inf.dend)) | ||
303 | { | ||
304 | len = (int)strlen(inf.filename); | ||
305 | if (sz <= 0) return len+1; | ||
306 | if (len >= sz) len=sz-1; | ||
307 | memcpy(path,inf.filename,len); | ||
308 | path[len++] = 0; | ||
309 | return len; | ||
310 | } | ||
311 | } | ||
312 | |||
313 | return -1; | ||
314 | } | ||
315 | #endif | ||
316 | |||
317 | #endif /* DSO_DL */ | ||