1 /* dso_win32.c -*- mode:C; c-file-style: "eay" -*- */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
62 #include <openssl/dso.h>
64 #if !defined(DSO_WIN32)
65 DSO_METHOD
*DSO_METHOD_win32(void)
73 static FARPROC
GetProcAddressA(HMODULE hModule
,LPCSTR lpProcName
)
75 WCHAR lpProcNameW
[64];
78 for (i
=0;lpProcName
[i
] && i
<64;i
++)
79 lpProcNameW
[i
] = (WCHAR
)lpProcName
[i
];
80 if (i
==64) return NULL
;
83 return GetProcAddressW(hModule
,lpProcNameW
);
86 # undef GetProcAddress
87 # define GetProcAddress GetProcAddressA
89 static HINSTANCE
LoadLibraryA(LPCSTR lpLibFileName
)
92 size_t len_0
=strlen(lpLibFileName
)+1,i
;
95 fnamw
= (WCHAR
*)_alloca (len_0
*sizeof(WCHAR
));
97 fnamw
= (WCHAR
*)alloca (len_0
*sizeof(WCHAR
));
101 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
++) fnamw
[i
]=(WCHAR
)lpLibFileName
[i
];
110 return LoadLibraryW(fnamw
);
114 /* Part of the hack in "win32_load" ... */
115 #define DSO_MAX_TRANSLATED_SIZE 256
117 static int win32_load(DSO
*dso
);
118 static int win32_unload(DSO
*dso
);
119 static void *win32_bind_var(DSO
*dso
, const char *symname
);
120 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
);
122 static int win32_unbind_var(DSO
*dso
, char *symname
, void *symptr
);
123 static int win32_unbind_func(DSO
*dso
, char *symname
, DSO_FUNC_TYPE symptr
);
124 static int win32_init(DSO
*dso
);
125 static int win32_finish(DSO
*dso
);
126 static long win32_ctrl(DSO
*dso
, int cmd
, long larg
, void *parg
);
128 static char *win32_name_converter(DSO
*dso
, const char *filename
);
129 static char *win32_merger(DSO
*dso
, const char *filespec1
,
130 const char *filespec2
);
131 static int win32_pathbyaddr(void *addr
,char *path
,int sz
);
132 static void *win32_globallookup(const char *name
);
134 static const char *openssl_strnchr(const char *string
, int c
, size_t len
);
136 static DSO_METHOD dso_meth_win32
= {
137 "OpenSSL 'win32' shared library method",
142 /* For now, "unbind" doesn't exist */
144 NULL
, /* unbind_var */
145 NULL
, /* unbind_func */
148 win32_name_converter
,
156 DSO_METHOD
*DSO_METHOD_win32(void)
158 return(&dso_meth_win32
);
161 /* For this DSO_METHOD, our meth_data STACK will contain;
162 * (i) a pointer to the handle (HINSTANCE) returned from
163 * LoadLibrary(), and copied.
166 static int win32_load(DSO
*dso
)
168 HINSTANCE h
= NULL
, *p
= NULL
;
169 /* See applicable comments from dso_dl.c */
170 char *filename
= DSO_convert_filename(dso
, NULL
);
174 DSOerr(DSO_F_WIN32_LOAD
,DSO_R_NO_FILENAME
);
177 h
= LoadLibraryA(filename
);
180 DSOerr(DSO_F_WIN32_LOAD
,DSO_R_LOAD_FAILED
);
181 ERR_add_error_data(3, "filename(", filename
, ")");
184 p
= (HINSTANCE
*)OPENSSL_malloc(sizeof(HINSTANCE
));
187 DSOerr(DSO_F_WIN32_LOAD
,ERR_R_MALLOC_FAILURE
);
191 if(!sk_void_push(dso
->meth_data
, p
))
193 DSOerr(DSO_F_WIN32_LOAD
,DSO_R_STACK_ERROR
);
197 dso
->loaded_filename
= filename
;
202 OPENSSL_free(filename
);
210 static int win32_unload(DSO
*dso
)
215 DSOerr(DSO_F_WIN32_UNLOAD
,ERR_R_PASSED_NULL_PARAMETER
);
218 if(sk_void_num(dso
->meth_data
) < 1)
220 p
= sk_void_pop(dso
->meth_data
);
223 DSOerr(DSO_F_WIN32_UNLOAD
,DSO_R_NULL_HANDLE
);
228 DSOerr(DSO_F_WIN32_UNLOAD
,DSO_R_UNLOAD_FAILED
);
229 /* We should push the value back onto the stack in
230 * case of a retry. */
231 sk_void_push(dso
->meth_data
, p
);
239 /* Using GetProcAddress for variables? TODO: Check this out in
240 * the Win32 API docs, there's probably a variant for variables. */
241 static void *win32_bind_var(DSO
*dso
, const char *symname
)
246 if((dso
== NULL
) || (symname
== NULL
))
248 DSOerr(DSO_F_WIN32_BIND_VAR
,ERR_R_PASSED_NULL_PARAMETER
);
251 if(sk_void_num(dso
->meth_data
) < 1)
253 DSOerr(DSO_F_WIN32_BIND_VAR
,DSO_R_STACK_ERROR
);
256 ptr
= sk_void_value(dso
->meth_data
, sk_void_num(dso
->meth_data
) - 1);
259 DSOerr(DSO_F_WIN32_BIND_VAR
,DSO_R_NULL_HANDLE
);
262 sym
= GetProcAddress(*ptr
, symname
);
265 DSOerr(DSO_F_WIN32_BIND_VAR
,DSO_R_SYM_FAILURE
);
266 ERR_add_error_data(3, "symname(", symname
, ")");
272 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
)
277 if((dso
== NULL
) || (symname
== NULL
))
279 DSOerr(DSO_F_WIN32_BIND_FUNC
,ERR_R_PASSED_NULL_PARAMETER
);
282 if(sk_void_num(dso
->meth_data
) < 1)
284 DSOerr(DSO_F_WIN32_BIND_FUNC
,DSO_R_STACK_ERROR
);
287 ptr
= sk_void_value(dso
->meth_data
, sk_void_num(dso
->meth_data
) - 1);
290 DSOerr(DSO_F_WIN32_BIND_FUNC
,DSO_R_NULL_HANDLE
);
293 sym
= GetProcAddress(*ptr
, symname
);
296 DSOerr(DSO_F_WIN32_BIND_FUNC
,DSO_R_SYM_FAILURE
);
297 ERR_add_error_data(3, "symname(", symname
, ")");
300 return((DSO_FUNC_TYPE
)sym
);
305 const char *node
; int nodelen
;
306 const char *device
; int devicelen
;
307 const char *predir
; int predirlen
;
308 const char *dir
; int dirlen
;
309 const char *file
; int filelen
;
312 static struct file_st
*win32_splitter(DSO
*dso
, const char *filename
,
313 int assume_last_is_dir
)
315 struct file_st
*result
= NULL
;
316 enum { IN_NODE
, IN_DEVICE
, IN_FILE
} position
;
317 const char *start
= filename
;
322 DSOerr(DSO_F_WIN32_SPLITTER
,DSO_R_NO_FILENAME
);
327 result
= OPENSSL_malloc(sizeof(struct file_st
));
330 DSOerr(DSO_F_WIN32_SPLITTER
,
331 ERR_R_MALLOC_FAILURE
);
335 memset(result
, 0, sizeof(struct file_st
));
336 position
= IN_DEVICE
;
338 if((filename
[0] == '\\' && filename
[1] == '\\')
339 || (filename
[0] == '/' && filename
[1] == '/'))
344 result
->node
= start
;
353 if(position
!= IN_DEVICE
)
355 DSOerr(DSO_F_WIN32_SPLITTER
,
356 DSO_R_INCORRECT_FILE_SYNTAX
);
358 OPENSSL_free(result
);
361 result
->device
= start
;
362 result
->devicelen
= (int)(filename
- start
);
369 if(position
== IN_NODE
)
371 result
->nodelen
= (int)(filename
- start
);
376 else if(position
== IN_DEVICE
)
381 result
->dirlen
= (int)(filename
- start
);
387 result
->dirlen
+= (int)(filename
- start
);
392 if(position
== IN_NODE
)
394 result
->nodelen
= (int)(filename
- start
);
398 if(filename
- start
> 0)
400 if (assume_last_is_dir
)
402 if (position
== IN_DEVICE
)
408 (int)(filename
- start
);
412 result
->file
= start
;
414 (int)(filename
- start
);
426 if(!result
->nodelen
) result
->node
= NULL
;
427 if(!result
->devicelen
) result
->device
= NULL
;
428 if(!result
->dirlen
) result
->dir
= NULL
;
429 if(!result
->filelen
) result
->file
= NULL
;
434 static char *win32_joiner(DSO
*dso
, const struct file_st
*file_split
)
436 int len
= 0, offset
= 0;
442 DSOerr(DSO_F_WIN32_JOINER
,
443 ERR_R_PASSED_NULL_PARAMETER
);
448 len
+= 2 + file_split
->nodelen
; /* 2 for starting \\ */
449 if(file_split
->predir
|| file_split
->dir
|| file_split
->file
)
450 len
++; /* 1 for ending \ */
452 else if(file_split
->device
)
454 len
+= file_split
->devicelen
+ 1; /* 1 for ending : */
456 len
+= file_split
->predirlen
;
457 if(file_split
->predir
&& (file_split
->dir
|| file_split
->file
))
459 len
++; /* 1 for ending \ */
461 len
+= file_split
->dirlen
;
462 if(file_split
->dir
&& file_split
->file
)
464 len
++; /* 1 for ending \ */
466 len
+= file_split
->filelen
;
470 DSOerr(DSO_F_WIN32_JOINER
, DSO_R_EMPTY_FILE_STRUCTURE
);
474 result
= OPENSSL_malloc(len
+ 1);
477 DSOerr(DSO_F_WIN32_JOINER
,
478 ERR_R_MALLOC_FAILURE
);
484 strcpy(&result
[offset
], "\\\\"); offset
+= 2;
485 strncpy(&result
[offset
], file_split
->node
,
486 file_split
->nodelen
); offset
+= file_split
->nodelen
;
487 if(file_split
->predir
|| file_split
->dir
|| file_split
->file
)
489 result
[offset
] = '\\'; offset
++;
492 else if(file_split
->device
)
494 strncpy(&result
[offset
], file_split
->device
,
495 file_split
->devicelen
); offset
+= file_split
->devicelen
;
496 result
[offset
] = ':'; offset
++;
498 start
= file_split
->predir
;
499 while(file_split
->predirlen
> (start
- file_split
->predir
))
501 const char *end
= openssl_strnchr(start
, '/',
502 file_split
->predirlen
- (start
- file_split
->predir
));
505 + file_split
->predirlen
506 - (start
- file_split
->predir
);
507 strncpy(&result
[offset
], start
,
508 end
- start
); offset
+= (int)(end
- start
);
509 result
[offset
] = '\\'; offset
++;
512 #if 0 /* Not needed, since the directory converter above already appeneded
514 if(file_split
->predir
&& (file_split
->dir
|| file_split
->file
))
516 result
[offset
] = '\\'; offset
++;
519 start
= file_split
->dir
;
520 while(file_split
->dirlen
> (start
- file_split
->dir
))
522 const char *end
= openssl_strnchr(start
, '/',
523 file_split
->dirlen
- (start
- file_split
->dir
));
527 - (start
- file_split
->dir
);
528 strncpy(&result
[offset
], start
,
529 end
- start
); offset
+= (int)(end
- start
);
530 result
[offset
] = '\\'; offset
++;
533 #if 0 /* Not needed, since the directory converter above already appeneded
535 if(file_split
->dir
&& file_split
->file
)
537 result
[offset
] = '\\'; offset
++;
540 strncpy(&result
[offset
], file_split
->file
,
541 file_split
->filelen
); offset
+= file_split
->filelen
;
542 result
[offset
] = '\0';
546 static char *win32_merger(DSO
*dso
, const char *filespec1
, const char *filespec2
)
549 struct file_st
*filespec1_split
= NULL
;
550 struct file_st
*filespec2_split
= NULL
;
552 if(!filespec1
&& !filespec2
)
554 DSOerr(DSO_F_WIN32_MERGER
,
555 ERR_R_PASSED_NULL_PARAMETER
);
560 merged
= OPENSSL_malloc(strlen(filespec1
) + 1);
563 DSOerr(DSO_F_WIN32_MERGER
,
564 ERR_R_MALLOC_FAILURE
);
567 strcpy(merged
, filespec1
);
571 merged
= OPENSSL_malloc(strlen(filespec2
) + 1);
574 DSOerr(DSO_F_WIN32_MERGER
,
575 ERR_R_MALLOC_FAILURE
);
578 strcpy(merged
, filespec2
);
582 filespec1_split
= win32_splitter(dso
, filespec1
, 0);
583 if (!filespec1_split
)
585 DSOerr(DSO_F_WIN32_MERGER
,
586 ERR_R_MALLOC_FAILURE
);
589 filespec2_split
= win32_splitter(dso
, filespec2
, 1);
590 if (!filespec2_split
)
592 DSOerr(DSO_F_WIN32_MERGER
,
593 ERR_R_MALLOC_FAILURE
);
594 OPENSSL_free(filespec1_split
);
598 /* Fill in into filespec1_split */
599 if (!filespec1_split
->node
&& !filespec1_split
->device
)
601 filespec1_split
->node
= filespec2_split
->node
;
602 filespec1_split
->nodelen
= filespec2_split
->nodelen
;
603 filespec1_split
->device
= filespec2_split
->device
;
604 filespec1_split
->devicelen
= filespec2_split
->devicelen
;
606 if (!filespec1_split
->dir
)
608 filespec1_split
->dir
= filespec2_split
->dir
;
609 filespec1_split
->dirlen
= filespec2_split
->dirlen
;
611 else if (filespec1_split
->dir
[0] != '\\'
612 && filespec1_split
->dir
[0] != '/')
614 filespec1_split
->predir
= filespec2_split
->dir
;
615 filespec1_split
->predirlen
= filespec2_split
->dirlen
;
617 if (!filespec1_split
->file
)
619 filespec1_split
->file
= filespec2_split
->file
;
620 filespec1_split
->filelen
= filespec2_split
->filelen
;
623 merged
= win32_joiner(dso
, filespec1_split
);
625 OPENSSL_free(filespec1_split
);
626 OPENSSL_free(filespec2_split
);
630 static char *win32_name_converter(DSO
*dso
, const char *filename
)
635 len
= strlen(filename
);
636 transform
= ((strstr(filename
, "/") == NULL
) &&
637 (strstr(filename
, "\\") == NULL
) &&
638 (strstr(filename
, ":") == NULL
));
640 /* We will convert this to "%s.dll" */
641 translated
= OPENSSL_malloc(len
+ 5);
643 /* We will simply duplicate filename */
644 translated
= OPENSSL_malloc(len
+ 1);
645 if(translated
== NULL
)
647 DSOerr(DSO_F_WIN32_NAME_CONVERTER
,
648 DSO_R_NAME_TRANSLATION_FAILED
);
652 sprintf(translated
, "%s.dll", filename
);
654 sprintf(translated
, "%s", filename
);
658 static const char *openssl_strnchr(const char *string
, int c
, size_t len
)
662 for (i
= 0, p
= string
; i
< len
&& *p
; i
++, p
++)
670 #include <tlhelp32.h>
672 # define DLLNAME "TOOLHELP.DLL"
674 # ifdef MODULEENTRY32
675 # undef MODULEENTRY32 /* unmask the ASCII version! */
677 # define DLLNAME "KERNEL32.DLL"
680 typedef HANDLE (WINAPI
*CREATETOOLHELP32SNAPSHOT
)(DWORD
, DWORD
);
681 typedef BOOL (WINAPI
*CLOSETOOLHELP32SNAPSHOT
)(HANDLE
);
682 typedef BOOL (WINAPI
*MODULE32
)(HANDLE
, MODULEENTRY32
*);
684 static int win32_pathbyaddr(void *addr
,char *path
,int sz
)
687 HANDLE hModuleSnap
= INVALID_HANDLE_VALUE
;
689 CREATETOOLHELP32SNAPSHOT create_snap
;
690 CLOSETOOLHELP32SNAPSHOT close_snap
;
691 MODULE32 module_first
, module_next
;
696 union { int(*f
)(void*,char*,int); void *p
; } t
=
697 { win32_pathbyaddr
};
701 dll
= LoadLibrary(TEXT(DLLNAME
));
704 DSOerr(DSO_F_WIN32_PATHBYADDR
,DSO_R_UNSUPPORTED
);
708 create_snap
= (CREATETOOLHELP32SNAPSHOT
)
709 GetProcAddress(dll
,"CreateToolhelp32Snapshot");
710 if (create_snap
== NULL
)
713 DSOerr(DSO_F_WIN32_PATHBYADDR
,DSO_R_UNSUPPORTED
);
716 /* We take the rest for granted... */
718 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)
719 GetProcAddress(dll
,"CloseToolhelp32Snapshot");
721 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)CloseHandle
;
723 module_first
= (MODULE32
)GetProcAddress(dll
,"Module32First");
724 module_next
= (MODULE32
)GetProcAddress(dll
,"Module32Next");
726 hModuleSnap
= (*create_snap
)(TH32CS_SNAPMODULE
,0);
727 if( hModuleSnap
== INVALID_HANDLE_VALUE
)
730 DSOerr(DSO_F_WIN32_PATHBYADDR
,DSO_R_UNSUPPORTED
);
734 me32
.dwSize
= sizeof(me32
);
736 if(!(*module_first
)(hModuleSnap
,&me32
))
738 (*close_snap
)(hModuleSnap
);
740 DSOerr(DSO_F_WIN32_PATHBYADDR
,DSO_R_FAILURE
);
745 if ((BYTE
*)addr
>= me32
.modBaseAddr
&&
746 (BYTE
*)addr
< me32
.modBaseAddr
+me32
.modBaseSize
)
748 (*close_snap
)(hModuleSnap
);
751 # if _WIN32_WCE >= 101
752 return WideCharToMultiByte(CP_ACP
,0,me32
.szExePath
,-1,
755 len
= (int)wcslen(me32
.szExePath
);
756 if (sz
<= 0) return len
+1;
757 if (len
>= sz
) len
=sz
-1;
759 path
[i
] = (char)me32
.szExePath
[i
];
764 len
= (int)strlen(me32
.szExePath
);
765 if (sz
<= 0) return len
+1;
766 if (len
>= sz
) len
=sz
-1;
767 memcpy(path
,me32
.szExePath
,len
);
772 } while((*module_next
)(hModuleSnap
, &me32
));
774 (*close_snap
)(hModuleSnap
);
779 static void *win32_globallookup(const char *name
)
782 HANDLE hModuleSnap
= INVALID_HANDLE_VALUE
;
784 CREATETOOLHELP32SNAPSHOT create_snap
;
785 CLOSETOOLHELP32SNAPSHOT close_snap
;
786 MODULE32 module_first
, module_next
;
789 dll
= LoadLibrary(TEXT(DLLNAME
));
792 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
,DSO_R_UNSUPPORTED
);
796 create_snap
= (CREATETOOLHELP32SNAPSHOT
)
797 GetProcAddress(dll
,"CreateToolhelp32Snapshot");
798 if (create_snap
== NULL
)
801 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
,DSO_R_UNSUPPORTED
);
804 /* We take the rest for granted... */
806 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)
807 GetProcAddress(dll
,"CloseToolhelp32Snapshot");
809 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)CloseHandle
;
811 module_first
= (MODULE32
)GetProcAddress(dll
,"Module32First");
812 module_next
= (MODULE32
)GetProcAddress(dll
,"Module32Next");
814 hModuleSnap
= (*create_snap
)(TH32CS_SNAPMODULE
,0);
815 if( hModuleSnap
== INVALID_HANDLE_VALUE
)
818 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
,DSO_R_UNSUPPORTED
);
822 me32
.dwSize
= sizeof(me32
);
824 if (!(*module_first
)(hModuleSnap
,&me32
))
826 (*close_snap
)(hModuleSnap
);
832 if ((ret
= GetProcAddress(me32
.hModule
,name
)))
834 (*close_snap
)(hModuleSnap
);
838 } while((*module_next
)(hModuleSnap
,&me32
));
840 (*close_snap
)(hModuleSnap
);
844 #endif /* DSO_WIN32 */