1 /* $OpenBSD: dso.h,v 1.12 2016/03/15 20:50:22 krw Exp $ */
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 module libressl_d.openssl.dso;
59 
60 
61 private static import core.stdc.config;
62 private static import libressl_d.openssl.ossl_typ;
63 public import libressl_d.openssl.crypto;
64 
65 extern (C):
66 nothrow @nogc:
67 
68 /* These values are used as commands to DSO_ctrl() */
69 enum DSO_CTRL_GET_FLAGS = 1;
70 enum DSO_CTRL_SET_FLAGS = 2;
71 enum DSO_CTRL_OR_FLAGS = 3;
72 
73 /**
74  * By default, DSO_load() will translate the provided filename into a form
75  * typical for the platform (more specifically the DSO_METHOD) using the
76  * dso_name_converter function of the method. Eg. win32 will transform "blah"
77  * into "blah.dll", and dlfcn will transform it into "libblah.so". The
78  * behaviour can be overridden by setting the name_converter callback in the DSO
79  * object (using DSO_set_name_converter()). This callback could even utilise
80  * the DSO_METHOD's converter too if it only wants to override behaviour for
81  * one or two possible DSO methods. However, the following flag can be set in a
82  * DSO to prevent *any* native name-translation at all - eg. if the caller has
83  * prompted the user for a path to a driver library so the filename should be
84  * interpreted as-is.
85  */
86 enum DSO_FLAG_NO_NAME_TRANSLATION = 0x01;
87 
88 /**
89  * An extra flag to give if only the extension should be added as
90  * translation.  This is obviously only of importance on Unix and
91  * other operating systems where the translation also may prefix
92  * the name with something, like 'lib', and ignored everywhere else.
93  * This flag is also ignored if DSO_FLAG_NO_NAME_TRANSLATION is used
94  * at the same time.
95  */
96 enum DSO_FLAG_NAME_TRANSLATION_EXT_ONLY = 0x02;
97 
98 /**
99  * The following flag controls the translation of symbol names to upper
100  * case.  This is currently only being implemented for OpenVMS.
101  */
102 enum DSO_FLAG_UPCASE_SYMBOL = 0x10;
103 
104 /**
105  * This flag loads the library with public symbols.
106  * Meaning: The exported symbols of this library are public
107  * to all libraries loaded after this library.
108  * At the moment only implemented in unix.
109  */
110 enum DSO_FLAG_GLOBAL_SYMBOLS = 0x20;
111 
112 alias DSO_FUNC_TYPE = extern (C) nothrow @nogc void function();
113 
114 alias DSO = .dso_st;
115 
116 /**
117  * The function prototype used for method functions (or caller-provided
118  * callbacks) that transform filenames. They are passed a DSO structure pointer
119  * (or null if they are to be used independantly of a DSO object) and a
120  * filename to transform. They should either return null (if there is an error
121  * condition) or a newly allocated string containing the transformed form that
122  * the caller will need to free with free() when done.
123  */
124 alias DSO_NAME_CONVERTER_FUNC = extern (C) nothrow @nogc char* function(.DSO*, const (char)*);
125 
126 /**
127  * The function prototype used for method functions (or caller-provided
128  * callbacks) that merge two file specifications. They are passed a
129  * DSO structure pointer (or null if they are to be used independantly of
130  * a DSO object) and two file specifications to merge. They should
131  * either return null (if there is an error condition) or a newly allocated
132  * string containing the result of merging that the caller will need
133  * to free with free() when done.
134  * Here, merging means that bits and pieces are taken from each of the
135  * file specifications and added together in whatever fashion that is
136  * sensible for the DSO method in question.  The only rule that really
137  * applies is that if the two specification contain pieces of the same
138  * type, the copy from the first string takes priority.  One could see
139  * it as the first specification is the one given by the user and the
140  * second being a bunch of defaults to add on if they're missing in the
141  * first.
142  */
143 alias DSO_MERGER_FUNC = extern (C) nothrow @nogc char* function(.DSO*, const (char)*, const (char)*);
144 
145 struct dso_meth_st
146 {
147 	const (char)* name;
148 
149 	/**
150 	 * Loads a shared library, NB: new DSO_METHODs must ensure that a
151 	 * successful load populates the loaded_filename field, and likewise a
152 	 * successful unload frees and NULLs it out.
153 	 */
154 	int function(.DSO* dso) dso_load;
155 
156 	/**
157 	 * Unloads a shared library
158 	 */
159 	int function(.DSO* dso) dso_unload;
160 
161 	/**
162 	 * Binds a variable
163 	 */
164 	void* function(.DSO* dso, const (char)* symname) dso_bind_var;
165 
166 	/**
167 	 * Binds a function - assumes a return type of DSO_FUNC_TYPE.
168 	 * This should be cast to the real function prototype by the
169 	 * caller. Platforms that don't have compatible representations
170 	 * for different prototypes (this is possible within ANSI C)
171 	 * are highly unlikely to have shared libraries at all, let
172 	 * alone a DSO_METHOD implemented for them.
173 	 */
174 	.DSO_FUNC_TYPE function(.DSO* dso, const (char)* symname) dso_bind_func;
175 
176 	/**
177 	 * The generic (yuck) "ctrl()" function. NB: Negative return
178 	 * values (rather than zero) indicate errors.
179 	 */
180 	core.stdc.config.c_long function(.DSO* dso, int cmd, core.stdc.config.c_long larg, void* parg) dso_ctrl;
181 
182 	/**
183 	 * The default DSO_METHOD-specific function for converting filenames to
184 	 * a canonical native form.
185 	 */
186 	.DSO_NAME_CONVERTER_FUNC dso_name_converter;
187 
188 	/**
189 	 * The default DSO_METHOD-specific function for converting filenames to
190 	 * a canonical native form.
191 	 */
192 	.DSO_MERGER_FUNC dso_merger;
193 
194 	/* [De]Initialisation handlers. */
195 	int function(.DSO* dso) init;
196 	int function(.DSO* dso) finish;
197 
198 	/**
199 	 * Return pathname of the module containing location
200 	 */
201 	int function(void* addr, char* path, int sz) pathbyaddr;
202 
203 	/**
204 	 * Perform global symbol lookup, i.e. among *all* modules
205 	 */
206 	void* function(const (char)* symname) globallookup;
207 }
208 
209 alias DSO_METHOD = .dso_meth_st;
210 
211 /* *********************************************************************/
212 /* The low-level handle type used to refer to a loaded shared library */
213 
214 struct dso_st
215 {
216 	.DSO_METHOD* meth;
217 
218 	/*
219 	 * Standard dlopen uses a (void *). Win32 uses a HANDLE. VMS
220 	 * doesn't use anything but will need to cache the filename
221 	 * for use in the dso_bind handler. All in all, let each
222 	 * method control its own destiny. "Handles" and such go in
223 	 * a STACK.
224 	 */
225 	libressl_d.openssl.crypto.stack_st_void* meth_data;
226 	int references;
227 	int flags;
228 
229 	/**
230 	 * For use by applications etc ... use this for your bits'n'pieces,
231 	 * don't touch meth_data!
232 	 */
233 	libressl_d.openssl.ossl_typ.CRYPTO_EX_DATA ex_data;
234 
235 	/**
236 	 * If this callback function pointer is set to non-null, then it will
237 	 * be used in DSO_load() in place of meth.dso_name_converter. NB: This
238 	 * should normally set using DSO_set_name_converter().
239 	 */
240 	.DSO_NAME_CONVERTER_FUNC name_converter;
241 
242 	/**
243 	 * If this callback function pointer is set to non-null, then it will
244 	 * be used in DSO_load() in place of meth.dso_merger. NB: This
245 	 * should normally set using DSO_set_merger().
246 	 */
247 	.DSO_MERGER_FUNC merger;
248 
249 	/**
250 	 * This is populated with (a copy of) the platform-independant
251 	 * filename used for this DSO.
252 	 */
253 	char* filename;
254 
255 	/**
256 	 * This is populated with (a copy of) the translated filename by which
257 	 * the DSO was actually loaded. It is null iff the DSO is not currently
258 	 * loaded. NB: This is here because the filename translation process
259 	 * may involve a callback being invoked more than once not only to
260 	 * convert to a platform-specific form, but also to try different
261 	 * filenames in the process of trying to perform a load. As such, this
262 	 * variable can be used to indicate (a) whether this DSO structure
263 	 * corresponds to a loaded library or not, and (b) the filename with
264 	 * which it was actually loaded.
265 	 */
266 	char* loaded_filename;
267 }
268 
269 .DSO* DSO_new();
270 .DSO* DSO_new_method(.DSO_METHOD* method);
271 int DSO_free(.DSO* dso);
272 int DSO_flags(.DSO* dso);
273 int DSO_up_ref(.DSO* dso);
274 core.stdc.config.c_long DSO_ctrl(.DSO* dso, int cmd, core.stdc.config.c_long larg, void* parg);
275 
276 /**
277  * This function sets the DSO's name_converter callback. If it is non-null,
278  * then it will be used instead of the associated DSO_METHOD's function. If
279  * oldcb is non-null then it is set to the function pointer value being
280  * replaced. Return value is non-zero for success.
281  */
282 int DSO_set_name_converter(.DSO* dso, .DSO_NAME_CONVERTER_FUNC cb, .DSO_NAME_CONVERTER_FUNC* oldcb);
283 
284 /*
285  * These functions can be used to get/set the platform-independant filename
286  * used for a DSO. NB: set will fail if the DSO is already loaded.
287  */
288 const (char)* DSO_get_filename(.DSO* dso);
289 int DSO_set_filename(.DSO* dso, const (char)* filename);
290 
291 /**
292  * This function will invoke the DSO's name_converter callback to translate a
293  * filename, or if the callback isn't set it will instead use the DSO_METHOD's
294  * converter. If "filename" is null, the "filename" in the DSO itself will be
295  * used. If the DSO_FLAG_NO_NAME_TRANSLATION flag is set, then the filename is
296  * simply duplicated. NB: This function is usually called from within a
297  * DSO_METHOD during the processing of a DSO_load() call, and is exposed so that
298  * caller-created DSO_METHODs can do the same thing. A non-null return value
299  * will need to be free()'d.
300  */
301 char* DSO_convert_filename(.DSO* dso, const (char)* filename);
302 
303 /**
304  * This function will invoke the DSO's merger callback to merge two file
305  * specifications, or if the callback isn't set it will instead use the
306  * DSO_METHOD's merger.  A non-null return value will need to be
307  * free()'d.
308  */
309 char* DSO_merge(.DSO* dso, const (char)* filespec1, const (char)* filespec2);
310 
311 /**
312  * If the DSO is currently loaded, this returns the filename that it was loaded
313  * under, otherwise it returns null. So it is also useful as a test as to
314  * whether the DSO is currently loaded. NB: This will not necessarily return
315  * the same value as DSO_convert_filename(dso, dso.filename), because the
316  * DSO_METHOD's load function may have tried a variety of filenames (with
317  * and/or without the aid of the converters) before settling on the one it
318  * actually loaded.
319  */
320 const (char)* DSO_get_loaded_filename(.DSO* dso);
321 
322 void DSO_set_default_method(.DSO_METHOD* meth);
323 .DSO_METHOD* DSO_get_default_method();
324 .DSO_METHOD* DSO_get_method(.DSO* dso);
325 .DSO_METHOD* DSO_set_method(.DSO* dso, .DSO_METHOD* meth);
326 
327 /**
328  * The all-singing all-dancing load function, you normally pass null
329  * for the first and third parameters. Use DSO_up and DSO_free for
330  * subsequent reference count handling. Any flags passed in will be set
331  * in the constructed DSO after its init() function but before the
332  * load operation. If 'dso' is non-null, 'flags' is ignored.
333  */
334 .DSO* DSO_load(.DSO* dso, const (char)* filename, .DSO_METHOD* meth, int flags);
335 
336 /**
337  * This function binds to a variable inside a shared library.
338  */
339 void* DSO_bind_var(.DSO* dso, const (char)* symname);
340 
341 /**
342  * This function binds to a function inside a shared library.
343  */
344 .DSO_FUNC_TYPE DSO_bind_func(.DSO* dso, const (char)* symname);
345 
346 /**
347  * This method is the default, but will beg, borrow, or steal whatever
348  * method should be the default on any particular platform (including
349  * DSO_METH_null() if necessary).
350  */
351 .DSO_METHOD* DSO_METHOD_openssl();
352 
353 /**
354  * This method is defined for all platforms - if a platform has no
355  * DSO support then this will be the only method!
356  */
357 .DSO_METHOD* DSO_METHOD_null();
358 
359 /**
360  * If DSO_DLFCN is defined, the standard dlfcn.h-style functions
361  * (dlopen, dlclose, dlsym, etc) will be used and incorporated into
362  * this method. If not, this method will return null.
363  */
364 .DSO_METHOD* DSO_METHOD_dlfcn();
365 
366 /**
367  * This function writes null-terminated pathname of DSO module
368  * containing 'addr' into 'sz' large caller-provided 'path' and
369  * returns the number of characters [including trailing zero]
370  * written to it. If 'sz' is 0 or negative, 'path' is ignored and
371  * required amount of charachers [including trailing zero] to
372  * accommodate pathname is returned. If 'addr' is null, then
373  * pathname of cryptolib itself is returned. Negative or zero
374  * return value denotes error.
375  */
376 int DSO_pathbyaddr(void* addr, char* path, int sz);
377 
378 /**
379  * This function should be used with caution! It looks up symbols in
380  * *all* loaded modules and if module gets unloaded by somebody else
381  * attempt to dereference the pointer is doomed to have fatal
382  * consequences. Primary usage for this function is to probe *core*
383  * system functionality, e.g. check if getnameinfo(3) is available
384  * at run-time without bothering about OS-specific details such as
385  * libc.so.versioning or where does it actually reside: in libc
386  * itself or libsocket.
387  */
388 void* DSO_global_lookup(const (char)* name);
389 
390 /* BEGIN ERROR CODES */
391 /**
392  * The following lines are auto generated by the script mkerr.pl. Any changes
393  * made after this point may be overwritten when the script is next run.
394  */
395 void ERR_load_DSO_strings();
396 
397 /* Error codes for the DSO functions. */
398 
399 /* Function codes. */
400 enum DSO_F_BEOS_BIND_FUNC = 144;
401 enum DSO_F_BEOS_BIND_VAR = 145;
402 enum DSO_F_BEOS_LOAD = 146;
403 enum DSO_F_BEOS_NAME_CONVERTER = 147;
404 enum DSO_F_BEOS_UNLOAD = 148;
405 enum DSO_F_DLFCN_BIND_FUNC = 100;
406 enum DSO_F_DLFCN_BIND_VAR = 101;
407 enum DSO_F_DLFCN_LOAD = 102;
408 enum DSO_F_DLFCN_MERGER = 130;
409 enum DSO_F_DLFCN_NAME_CONVERTER = 123;
410 enum DSO_F_DLFCN_UNLOAD = 103;
411 enum DSO_F_DL_BIND_FUNC = 104;
412 enum DSO_F_DL_BIND_VAR = 105;
413 enum DSO_F_DL_LOAD = 106;
414 enum DSO_F_DL_MERGER = 131;
415 enum DSO_F_DL_NAME_CONVERTER = 124;
416 enum DSO_F_DL_UNLOAD = 107;
417 enum DSO_F_DSO_BIND_FUNC = 108;
418 enum DSO_F_DSO_BIND_VAR = 109;
419 enum DSO_F_DSO_CONVERT_FILENAME = 126;
420 enum DSO_F_DSO_CTRL = 110;
421 enum DSO_F_DSO_FREE = 111;
422 enum DSO_F_DSO_GET_FILENAME = 127;
423 enum DSO_F_DSO_GET_LOADED_FILENAME = 128;
424 enum DSO_F_DSO_GLOBAL_LOOKUP = 139;
425 enum DSO_F_DSO_LOAD = 112;
426 enum DSO_F_DSO_MERGE = 132;
427 enum DSO_F_DSO_NEW_METHOD = 113;
428 enum DSO_F_DSO_PATHBYADDR = 140;
429 enum DSO_F_DSO_SET_FILENAME = 129;
430 enum DSO_F_DSO_SET_NAME_CONVERTER = 122;
431 enum DSO_F_DSO_UP_REF = 114;
432 enum DSO_F_GLOBAL_LOOKUP_FUNC = 138;
433 enum DSO_F_PATHBYADDR = 137;
434 enum DSO_F_VMS_BIND_SYM = 115;
435 enum DSO_F_VMS_LOAD = 116;
436 enum DSO_F_VMS_MERGER = 133;
437 enum DSO_F_VMS_UNLOAD = 117;
438 enum DSO_F_WIN32_BIND_FUNC = 118;
439 enum DSO_F_WIN32_BIND_VAR = 119;
440 enum DSO_F_WIN32_GLOBALLOOKUP = 142;
441 enum DSO_F_WIN32_GLOBALLOOKUP_FUNC = 143;
442 enum DSO_F_WIN32_JOINER = 135;
443 enum DSO_F_WIN32_LOAD = 120;
444 enum DSO_F_WIN32_MERGER = 134;
445 enum DSO_F_WIN32_NAME_CONVERTER = 125;
446 enum DSO_F_WIN32_PATHBYADDR = 141;
447 enum DSO_F_WIN32_SPLITTER = 136;
448 enum DSO_F_WIN32_UNLOAD = 121;
449 
450 /* Reason codes. */
451 enum DSO_R_CTRL_FAILED = 100;
452 enum DSO_R_DSO_ALREADY_LOADED = 110;
453 enum DSO_R_EMPTY_FILE_STRUCTURE = 113;
454 enum DSO_R_FAILURE = 114;
455 enum DSO_R_FILENAME_TOO_BIG = 101;
456 enum DSO_R_FINISH_FAILED = 102;
457 enum DSO_R_INCORRECT_FILE_SYNTAX = 115;
458 enum DSO_R_LOAD_FAILED = 103;
459 enum DSO_R_NAME_TRANSLATION_FAILED = 109;
460 enum DSO_R_NO_FILENAME = 111;
461 enum DSO_R_NO_FILE_SPECIFICATION = 116;
462 enum DSO_R_NULL_HANDLE = 104;
463 enum DSO_R_SET_FILENAME_FAILED = 112;
464 enum DSO_R_STACK_ERROR = 105;
465 enum DSO_R_SYM_FAILURE = 106;
466 enum DSO_R_UNLOAD_FAILED = 107;
467 enum DSO_R_UNSUPPORTED = 108;