3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/dso.h>
65 #if !defined(DSO_WIN32)
66 DSO_METHOD
*DSO_METHOD_win32(void)
74 static FARPROC
GetProcAddressA(HMODULE hModule
, LPCSTR lpProcName
)
76 WCHAR lpProcNameW
[64];
79 for (i
= 0; lpProcName
[i
] && i
< 64; i
++)
80 lpProcNameW
[i
] = (WCHAR
)lpProcName
[i
];
85 return GetProcAddressW(hModule
, lpProcNameW
);
88 # undef GetProcAddress
89 # define GetProcAddress GetProcAddressA
91 static HINSTANCE
LoadLibraryA(LPCSTR lpLibFileName
)
94 size_t len_0
= strlen(lpLibFileName
) + 1, i
;
97 fnamw
= (WCHAR
*)_alloca(len_0
* sizeof(WCHAR
));
99 fnamw
= (WCHAR
*)alloca(len_0
* sizeof(WCHAR
));
102 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
105 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
106 if (!MultiByteToWideChar(CP_ACP
, 0, lpLibFileName
, len_0
, fnamw
, len_0
))
108 for (i
= 0; i
< len_0
; i
++)
109 fnamw
[i
] = (WCHAR
)lpLibFileName
[i
];
111 return LoadLibraryW(fnamw
);
115 /* Part of the hack in "win32_load" ... */
116 # define DSO_MAX_TRANSLATED_SIZE 256
118 static int win32_load(DSO
*dso
);
119 static int win32_unload(DSO
*dso
);
120 static void *win32_bind_var(DSO
*dso
, const char *symname
);
121 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
);
123 static int win32_unbind_var(DSO
*dso
, char *symname
, void *symptr
);
124 static int win32_unbind_func(DSO
*dso
, char *symname
, DSO_FUNC_TYPE symptr
);
125 static int win32_init(DSO
*dso
);
126 static int win32_finish(DSO
*dso
);
127 static long win32_ctrl(DSO
*dso
, int cmd
, long larg
, void *parg
);
129 static char *win32_name_converter(DSO
*dso
, const char *filename
);
130 static char *win32_merger(DSO
*dso
, const char *filespec1
,
131 const char *filespec2
);
132 static int win32_pathbyaddr(void *addr
, char *path
, int sz
);
133 static void *win32_globallookup(const char *name
);
135 static const char *openssl_strnchr(const char *string
, int c
, size_t len
);
137 static DSO_METHOD dso_meth_win32
= {
138 "OpenSSL 'win32' shared library method",
143 /* For now, "unbind" doesn't exist */
145 NULL
, /* unbind_var */
146 NULL
, /* unbind_func */
149 win32_name_converter
,
157 DSO_METHOD
*DSO_METHOD_win32(void)
159 return (&dso_meth_win32
);
163 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
164 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
167 static int win32_load(DSO
*dso
)
169 HINSTANCE h
= NULL
, *p
= NULL
;
170 /* See applicable comments from dso_dl.c */
171 char *filename
= DSO_convert_filename(dso
, NULL
);
173 if (filename
== NULL
) {
174 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_NO_FILENAME
);
177 h
= LoadLibraryA(filename
);
179 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_LOAD_FAILED
);
180 ERR_add_error_data(3, "filename(", filename
, ")");
183 p
= (HINSTANCE
*) OPENSSL_malloc(sizeof(HINSTANCE
));
185 DSOerr(DSO_F_WIN32_LOAD
, ERR_R_MALLOC_FAILURE
);
189 if (!sk_void_push(dso
->meth_data
, p
)) {
190 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_STACK_ERROR
);
194 dso
->loaded_filename
= filename
;
198 if (filename
!= NULL
)
199 OPENSSL_free(filename
);
207 static int win32_unload(DSO
*dso
)
211 DSOerr(DSO_F_WIN32_UNLOAD
, ERR_R_PASSED_NULL_PARAMETER
);
214 if (sk_void_num(dso
->meth_data
) < 1)
216 p
= sk_void_pop(dso
->meth_data
);
218 DSOerr(DSO_F_WIN32_UNLOAD
, DSO_R_NULL_HANDLE
);
221 if (!FreeLibrary(*p
)) {
222 DSOerr(DSO_F_WIN32_UNLOAD
, DSO_R_UNLOAD_FAILED
);
224 * We should push the value back onto the stack in case of a retry.
226 sk_void_push(dso
->meth_data
, p
);
235 * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
236 * docs, there's probably a variant for variables.
238 static void *win32_bind_var(DSO
*dso
, const char *symname
)
243 if ((dso
== NULL
) || (symname
== NULL
)) {
244 DSOerr(DSO_F_WIN32_BIND_VAR
, ERR_R_PASSED_NULL_PARAMETER
);
247 if (sk_void_num(dso
->meth_data
) < 1) {
248 DSOerr(DSO_F_WIN32_BIND_VAR
, DSO_R_STACK_ERROR
);
251 ptr
= sk_void_value(dso
->meth_data
, sk_void_num(dso
->meth_data
) - 1);
253 DSOerr(DSO_F_WIN32_BIND_VAR
, DSO_R_NULL_HANDLE
);
256 sym
= GetProcAddress(*ptr
, symname
);
258 DSOerr(DSO_F_WIN32_BIND_VAR
, DSO_R_SYM_FAILURE
);
259 ERR_add_error_data(3, "symname(", symname
, ")");
265 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
)
270 if ((dso
== NULL
) || (symname
== NULL
)) {
271 DSOerr(DSO_F_WIN32_BIND_FUNC
, ERR_R_PASSED_NULL_PARAMETER
);
274 if (sk_void_num(dso
->meth_data
) < 1) {
275 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_STACK_ERROR
);
278 ptr
= sk_void_value(dso
->meth_data
, sk_void_num(dso
->meth_data
) - 1);
280 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_NULL_HANDLE
);
283 sym
= GetProcAddress(*ptr
, symname
);
285 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_SYM_FAILURE
);
286 ERR_add_error_data(3, "symname(", symname
, ")");
289 return ((DSO_FUNC_TYPE
)sym
);
305 static struct file_st
*win32_splitter(DSO
*dso
, const char *filename
,
306 int assume_last_is_dir
)
308 struct file_st
*result
= NULL
;
309 enum { IN_NODE
, IN_DEVICE
, IN_FILE
} position
;
310 const char *start
= filename
;
314 DSOerr(DSO_F_WIN32_SPLITTER
, DSO_R_NO_FILENAME
);
321 result
= OPENSSL_malloc(sizeof(struct file_st
));
322 if (result
== NULL
) {
323 DSOerr(DSO_F_WIN32_SPLITTER
, ERR_R_MALLOC_FAILURE
);
327 memset(result
, 0, sizeof(struct file_st
));
328 position
= IN_DEVICE
;
330 if ((filename
[0] == '\\' && filename
[1] == '\\')
331 || (filename
[0] == '/' && filename
[1] == '/')) {
335 result
->node
= start
;
342 if (position
!= IN_DEVICE
) {
343 DSOerr(DSO_F_WIN32_SPLITTER
, DSO_R_INCORRECT_FILE_SYNTAX
);
347 OPENSSL_free(result
);
350 result
->device
= start
;
351 result
->devicelen
= (int)(filename
- start
);
358 if (position
== IN_NODE
) {
359 result
->nodelen
= (int)(filename
- start
);
363 } else if (position
== IN_DEVICE
) {
367 result
->dirlen
= (int)(filename
- start
);
371 result
->dirlen
+= (int)(filename
- start
);
376 if (position
== IN_NODE
) {
377 result
->nodelen
= (int)(filename
- start
);
379 if (filename
- start
> 0) {
380 if (assume_last_is_dir
) {
381 if (position
== IN_DEVICE
) {
385 result
->dirlen
+= (int)(filename
- start
);
387 result
->file
= start
;
388 result
->filelen
= (int)(filename
- start
);
400 if (!result
->nodelen
)
402 if (!result
->devicelen
)
403 result
->device
= NULL
;
406 if (!result
->filelen
)
412 static char *win32_joiner(DSO
*dso
, const struct file_st
*file_split
)
414 int len
= 0, offset
= 0;
419 DSOerr(DSO_F_WIN32_JOINER
, ERR_R_PASSED_NULL_PARAMETER
);
422 if (file_split
->node
) {
423 len
+= 2 + file_split
->nodelen
; /* 2 for starting \\ */
424 if (file_split
->predir
|| file_split
->dir
|| file_split
->file
)
425 len
++; /* 1 for ending \ */
426 } else if (file_split
->device
) {
427 len
+= file_split
->devicelen
+ 1; /* 1 for ending : */
429 len
+= file_split
->predirlen
;
430 if (file_split
->predir
&& (file_split
->dir
|| file_split
->file
)) {
431 len
++; /* 1 for ending \ */
433 len
+= file_split
->dirlen
;
434 if (file_split
->dir
&& file_split
->file
) {
435 len
++; /* 1 for ending \ */
437 len
+= file_split
->filelen
;
440 DSOerr(DSO_F_WIN32_JOINER
, DSO_R_EMPTY_FILE_STRUCTURE
);
444 result
= OPENSSL_malloc(len
+ 1);
446 DSOerr(DSO_F_WIN32_JOINER
, ERR_R_MALLOC_FAILURE
);
450 if (file_split
->node
) {
451 strcpy(&result
[offset
], "\\\\");
453 strncpy(&result
[offset
], file_split
->node
, file_split
->nodelen
);
454 offset
+= file_split
->nodelen
;
455 if (file_split
->predir
|| file_split
->dir
|| file_split
->file
) {
456 result
[offset
] = '\\';
459 } else if (file_split
->device
) {
460 strncpy(&result
[offset
], file_split
->device
, file_split
->devicelen
);
461 offset
+= file_split
->devicelen
;
462 result
[offset
] = ':';
465 start
= file_split
->predir
;
466 while (file_split
->predirlen
> (start
- file_split
->predir
)) {
467 const char *end
= openssl_strnchr(start
, '/',
468 file_split
->predirlen
- (start
-
469 file_split
->predir
));
472 + file_split
->predirlen
- (start
- file_split
->predir
);
473 strncpy(&result
[offset
], start
, end
- start
);
474 offset
+= (int)(end
- start
);
475 result
[offset
] = '\\';
479 # if 0 /* Not needed, since the directory converter
480 * above already appeneded a backslash */
481 if (file_split
->predir
&& (file_split
->dir
|| file_split
->file
)) {
482 result
[offset
] = '\\';
486 start
= file_split
->dir
;
487 while (file_split
->dirlen
> (start
- file_split
->dir
)) {
488 const char *end
= openssl_strnchr(start
, '/',
489 file_split
->dirlen
- (start
-
492 end
= start
+ file_split
->dirlen
- (start
- file_split
->dir
);
493 strncpy(&result
[offset
], start
, end
- start
);
494 offset
+= (int)(end
- start
);
495 result
[offset
] = '\\';
499 # if 0 /* Not needed, since the directory converter
500 * above already appeneded a backslash */
501 if (file_split
->dir
&& file_split
->file
) {
502 result
[offset
] = '\\';
506 strncpy(&result
[offset
], file_split
->file
, file_split
->filelen
);
507 offset
+= file_split
->filelen
;
508 result
[offset
] = '\0';
512 static char *win32_merger(DSO
*dso
, const char *filespec1
,
513 const char *filespec2
)
516 struct file_st
*filespec1_split
= NULL
;
517 struct file_st
*filespec2_split
= NULL
;
519 if (!filespec1
&& !filespec2
) {
520 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_PASSED_NULL_PARAMETER
);
524 merged
= OPENSSL_malloc(strlen(filespec1
) + 1);
526 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
529 strcpy(merged
, filespec1
);
530 } else if (!filespec1
) {
531 merged
= OPENSSL_malloc(strlen(filespec2
) + 1);
533 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
536 strcpy(merged
, filespec2
);
538 filespec1_split
= win32_splitter(dso
, filespec1
, 0);
539 if (!filespec1_split
) {
540 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
543 filespec2_split
= win32_splitter(dso
, filespec2
, 1);
544 if (!filespec2_split
) {
545 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
546 OPENSSL_free(filespec1_split
);
550 /* Fill in into filespec1_split */
551 if (!filespec1_split
->node
&& !filespec1_split
->device
) {
552 filespec1_split
->node
= filespec2_split
->node
;
553 filespec1_split
->nodelen
= filespec2_split
->nodelen
;
554 filespec1_split
->device
= filespec2_split
->device
;
555 filespec1_split
->devicelen
= filespec2_split
->devicelen
;
557 if (!filespec1_split
->dir
) {
558 filespec1_split
->dir
= filespec2_split
->dir
;
559 filespec1_split
->dirlen
= filespec2_split
->dirlen
;
560 } else if (filespec1_split
->dir
[0] != '\\'
561 && filespec1_split
->dir
[0] != '/') {
562 filespec1_split
->predir
= filespec2_split
->dir
;
563 filespec1_split
->predirlen
= filespec2_split
->dirlen
;
565 if (!filespec1_split
->file
) {
566 filespec1_split
->file
= filespec2_split
->file
;
567 filespec1_split
->filelen
= filespec2_split
->filelen
;
570 merged
= win32_joiner(dso
, filespec1_split
);
572 OPENSSL_free(filespec1_split
);
573 OPENSSL_free(filespec2_split
);
577 static char *win32_name_converter(DSO
*dso
, const char *filename
)
582 len
= strlen(filename
);
583 transform
= ((strstr(filename
, "/") == NULL
) &&
584 (strstr(filename
, "\\") == NULL
) &&
585 (strstr(filename
, ":") == NULL
));
587 /* We will convert this to "%s.dll" */
588 translated
= OPENSSL_malloc(len
+ 5);
590 /* We will simply duplicate filename */
591 translated
= OPENSSL_malloc(len
+ 1);
592 if (translated
== NULL
) {
593 DSOerr(DSO_F_WIN32_NAME_CONVERTER
, DSO_R_NAME_TRANSLATION_FAILED
);
597 sprintf(translated
, "%s.dll", filename
);
599 sprintf(translated
, "%s", filename
);
603 static const char *openssl_strnchr(const char *string
, int c
, size_t len
)
607 for (i
= 0, p
= string
; i
< len
&& *p
; i
++, p
++) {
614 # include <tlhelp32.h>
616 # define DLLNAME "TOOLHELP.DLL"
618 # ifdef MODULEENTRY32
619 # undef MODULEENTRY32 /* unmask the ASCII version! */
621 # define DLLNAME "KERNEL32.DLL"
624 typedef HANDLE(WINAPI
*CREATETOOLHELP32SNAPSHOT
) (DWORD
, DWORD
);
625 typedef BOOL(WINAPI
*CLOSETOOLHELP32SNAPSHOT
) (HANDLE
);
626 typedef BOOL(WINAPI
*MODULE32
) (HANDLE
, MODULEENTRY32
*);
628 static int win32_pathbyaddr(void *addr
, char *path
, int sz
)
631 HANDLE hModuleSnap
= INVALID_HANDLE_VALUE
;
633 CREATETOOLHELP32SNAPSHOT create_snap
;
634 CLOSETOOLHELP32SNAPSHOT close_snap
;
635 MODULE32 module_first
, module_next
;
640 int (*f
) (void *, char *, int);
648 dll
= LoadLibrary(TEXT(DLLNAME
));
650 DSOerr(DSO_F_WIN32_PATHBYADDR
, DSO_R_UNSUPPORTED
);
654 create_snap
= (CREATETOOLHELP32SNAPSHOT
)
655 GetProcAddress(dll
, "CreateToolhelp32Snapshot");
656 if (create_snap
== NULL
) {
658 DSOerr(DSO_F_WIN32_PATHBYADDR
, DSO_R_UNSUPPORTED
);
661 /* We take the rest for granted... */
663 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)
664 GetProcAddress(dll
, "CloseToolhelp32Snapshot");
666 close_snap
= (CLOSETOOLHELP32SNAPSHOT
) CloseHandle
;
668 module_first
= (MODULE32
) GetProcAddress(dll
, "Module32First");
669 module_next
= (MODULE32
) GetProcAddress(dll
, "Module32Next");
671 hModuleSnap
= (*create_snap
) (TH32CS_SNAPMODULE
, 0);
672 if (hModuleSnap
== INVALID_HANDLE_VALUE
) {
674 DSOerr(DSO_F_WIN32_PATHBYADDR
, DSO_R_UNSUPPORTED
);
678 me32
.dwSize
= sizeof(me32
);
680 if (!(*module_first
) (hModuleSnap
, &me32
)) {
681 (*close_snap
) (hModuleSnap
);
683 DSOerr(DSO_F_WIN32_PATHBYADDR
, DSO_R_FAILURE
);
688 if ((BYTE
*) addr
>= me32
.modBaseAddr
&&
689 (BYTE
*) addr
< me32
.modBaseAddr
+ me32
.modBaseSize
) {
690 (*close_snap
) (hModuleSnap
);
693 # if _WIN32_WCE >= 101
694 return WideCharToMultiByte(CP_ACP
, 0, me32
.szExePath
, -1,
695 path
, sz
, NULL
, NULL
);
697 len
= (int)wcslen(me32
.szExePath
);
702 for (i
= 0; i
< len
; i
++)
703 path
[i
] = (char)me32
.szExePath
[i
];
708 len
= (int)strlen(me32
.szExePath
);
713 memcpy(path
, me32
.szExePath
, len
);
718 } while ((*module_next
) (hModuleSnap
, &me32
));
720 (*close_snap
) (hModuleSnap
);
725 static void *win32_globallookup(const char *name
)
728 HANDLE hModuleSnap
= INVALID_HANDLE_VALUE
;
730 CREATETOOLHELP32SNAPSHOT create_snap
;
731 CLOSETOOLHELP32SNAPSHOT close_snap
;
732 MODULE32 module_first
, module_next
;
735 dll
= LoadLibrary(TEXT(DLLNAME
));
737 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
741 create_snap
= (CREATETOOLHELP32SNAPSHOT
)
742 GetProcAddress(dll
, "CreateToolhelp32Snapshot");
743 if (create_snap
== NULL
) {
745 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
748 /* We take the rest for granted... */
750 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)
751 GetProcAddress(dll
, "CloseToolhelp32Snapshot");
753 close_snap
= (CLOSETOOLHELP32SNAPSHOT
) CloseHandle
;
755 module_first
= (MODULE32
) GetProcAddress(dll
, "Module32First");
756 module_next
= (MODULE32
) GetProcAddress(dll
, "Module32Next");
758 hModuleSnap
= (*create_snap
) (TH32CS_SNAPMODULE
, 0);
759 if (hModuleSnap
== INVALID_HANDLE_VALUE
) {
761 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
765 me32
.dwSize
= sizeof(me32
);
767 if (!(*module_first
) (hModuleSnap
, &me32
)) {
768 (*close_snap
) (hModuleSnap
);
774 if ((ret
= GetProcAddress(me32
.hModule
, name
))) {
775 (*close_snap
) (hModuleSnap
);
779 } while ((*module_next
) (hModuleSnap
, &me32
));
781 (*close_snap
) (hModuleSnap
);
785 #endif /* DSO_WIN32 */