2 * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #if defined(DSO_WIN32)
16 static FARPROC
GetProcAddressA(HMODULE hModule
, LPCSTR lpProcName
)
18 WCHAR lpProcNameW
[64];
21 for (i
= 0; lpProcName
[i
] && i
< 64; i
++)
22 lpProcNameW
[i
] = (WCHAR
)lpProcName
[i
];
27 return GetProcAddressW(hModule
, lpProcNameW
);
30 # undef GetProcAddress
31 # define GetProcAddress GetProcAddressA
33 static HINSTANCE
LoadLibraryA(LPCSTR lpLibFileName
)
36 size_t len_0
= strlen(lpLibFileName
) + 1, i
;
39 fnamw
= (WCHAR
*)_alloca(len_0
* sizeof(WCHAR
));
41 fnamw
= (WCHAR
*)alloca(len_0
* sizeof(WCHAR
));
44 SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
47 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
48 if (!MultiByteToWideChar(CP_ACP
, 0, lpLibFileName
, len_0
, fnamw
, len_0
))
50 for (i
= 0; i
< len_0
; i
++)
51 fnamw
[i
] = (WCHAR
)lpLibFileName
[i
];
53 return LoadLibraryW(fnamw
);
57 /* Part of the hack in "win32_load" ... */
58 # define DSO_MAX_TRANSLATED_SIZE 256
60 static int win32_load(DSO
*dso
);
61 static int win32_unload(DSO
*dso
);
62 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
);
63 static char *win32_name_converter(DSO
*dso
, const char *filename
);
64 static char *win32_merger(DSO
*dso
, const char *filespec1
,
65 const char *filespec2
);
66 static void *win32_globallookup(const char *name
);
68 static const char *openssl_strnchr(const char *string
, int c
, size_t len
);
70 static DSO_METHOD dso_meth_win32
= {
71 "OpenSSL 'win32' shared library method",
83 DSO_METHOD
*DSO_METHOD_openssl(void)
85 return &dso_meth_win32
;
89 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
90 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
93 static int win32_load(DSO
*dso
)
95 HINSTANCE h
= NULL
, *p
= NULL
;
96 /* See applicable comments from dso_dl.c */
97 char *filename
= DSO_convert_filename(dso
, NULL
);
99 if (filename
== NULL
) {
100 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_NO_FILENAME
);
103 h
= LoadLibraryA(filename
);
105 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_LOAD_FAILED
);
106 ERR_add_error_data(3, "filename(", filename
, ")");
109 p
= OPENSSL_malloc(sizeof(*p
));
111 DSOerr(DSO_F_WIN32_LOAD
, ERR_R_MALLOC_FAILURE
);
115 if (!sk_void_push(dso
->meth_data
, p
)) {
116 DSOerr(DSO_F_WIN32_LOAD
, DSO_R_STACK_ERROR
);
120 dso
->loaded_filename
= filename
;
124 OPENSSL_free(filename
);
131 static int win32_unload(DSO
*dso
)
135 DSOerr(DSO_F_WIN32_UNLOAD
, ERR_R_PASSED_NULL_PARAMETER
);
138 if (sk_void_num(dso
->meth_data
) < 1)
140 p
= sk_void_pop(dso
->meth_data
);
142 DSOerr(DSO_F_WIN32_UNLOAD
, DSO_R_NULL_HANDLE
);
145 if (!FreeLibrary(*p
)) {
146 DSOerr(DSO_F_WIN32_UNLOAD
, DSO_R_UNLOAD_FAILED
);
148 * We should push the value back onto the stack in case of a retry.
150 sk_void_push(dso
->meth_data
, p
);
158 static DSO_FUNC_TYPE
win32_bind_func(DSO
*dso
, const char *symname
)
166 if ((dso
== NULL
) || (symname
== NULL
)) {
167 DSOerr(DSO_F_WIN32_BIND_FUNC
, ERR_R_PASSED_NULL_PARAMETER
);
170 if (sk_void_num(dso
->meth_data
) < 1) {
171 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_STACK_ERROR
);
174 ptr
= sk_void_value(dso
->meth_data
, sk_void_num(dso
->meth_data
) - 1);
176 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_NULL_HANDLE
);
179 sym
.f
= GetProcAddress(*ptr
, symname
);
181 DSOerr(DSO_F_WIN32_BIND_FUNC
, DSO_R_SYM_FAILURE
);
182 ERR_add_error_data(3, "symname(", symname
, ")");
185 return ((DSO_FUNC_TYPE
)sym
.f
);
201 static struct file_st
*win32_splitter(DSO
*dso
, const char *filename
,
202 int assume_last_is_dir
)
204 struct file_st
*result
= NULL
;
205 enum { IN_NODE
, IN_DEVICE
, IN_FILE
} position
;
206 const char *start
= filename
;
210 DSOerr(DSO_F_WIN32_SPLITTER
, DSO_R_NO_FILENAME
);
217 result
= OPENSSL_zalloc(sizeof(*result
));
218 if (result
== NULL
) {
219 DSOerr(DSO_F_WIN32_SPLITTER
, ERR_R_MALLOC_FAILURE
);
223 position
= IN_DEVICE
;
225 if ((filename
[0] == '\\' && filename
[1] == '\\')
226 || (filename
[0] == '/' && filename
[1] == '/')) {
230 result
->node
= start
;
237 if (position
!= IN_DEVICE
) {
238 DSOerr(DSO_F_WIN32_SPLITTER
, DSO_R_INCORRECT_FILE_SYNTAX
);
242 OPENSSL_free(result
);
245 result
->device
= start
;
246 result
->devicelen
= (int)(filename
- start
);
253 if (position
== IN_NODE
) {
254 result
->nodelen
= (int)(filename
- start
);
258 } else if (position
== IN_DEVICE
) {
262 result
->dirlen
= (int)(filename
- start
);
266 result
->dirlen
+= (int)(filename
- start
);
271 if (position
== IN_NODE
) {
272 result
->nodelen
= (int)(filename
- start
);
274 if (filename
- start
> 0) {
275 if (assume_last_is_dir
) {
276 if (position
== IN_DEVICE
) {
280 result
->dirlen
+= (int)(filename
- start
);
282 result
->file
= start
;
283 result
->filelen
= (int)(filename
- start
);
295 if (!result
->nodelen
)
297 if (!result
->devicelen
)
298 result
->device
= NULL
;
301 if (!result
->filelen
)
307 static char *win32_joiner(DSO
*dso
, const struct file_st
*file_split
)
309 int len
= 0, offset
= 0;
314 DSOerr(DSO_F_WIN32_JOINER
, ERR_R_PASSED_NULL_PARAMETER
);
317 if (file_split
->node
) {
318 len
+= 2 + file_split
->nodelen
; /* 2 for starting \\ */
319 if (file_split
->predir
|| file_split
->dir
|| file_split
->file
)
320 len
++; /* 1 for ending \ */
321 } else if (file_split
->device
) {
322 len
+= file_split
->devicelen
+ 1; /* 1 for ending : */
324 len
+= file_split
->predirlen
;
325 if (file_split
->predir
&& (file_split
->dir
|| file_split
->file
)) {
326 len
++; /* 1 for ending \ */
328 len
+= file_split
->dirlen
;
329 if (file_split
->dir
&& file_split
->file
) {
330 len
++; /* 1 for ending \ */
332 len
+= file_split
->filelen
;
335 DSOerr(DSO_F_WIN32_JOINER
, DSO_R_EMPTY_FILE_STRUCTURE
);
339 result
= OPENSSL_malloc(len
+ 1);
340 if (result
== NULL
) {
341 DSOerr(DSO_F_WIN32_JOINER
, ERR_R_MALLOC_FAILURE
);
345 if (file_split
->node
) {
346 strcpy(&result
[offset
], "\\\\");
348 strncpy(&result
[offset
], file_split
->node
, file_split
->nodelen
);
349 offset
+= file_split
->nodelen
;
350 if (file_split
->predir
|| file_split
->dir
|| file_split
->file
) {
351 result
[offset
] = '\\';
354 } else if (file_split
->device
) {
355 strncpy(&result
[offset
], file_split
->device
, file_split
->devicelen
);
356 offset
+= file_split
->devicelen
;
357 result
[offset
] = ':';
360 start
= file_split
->predir
;
361 while (file_split
->predirlen
> (start
- file_split
->predir
)) {
362 const char *end
= openssl_strnchr(start
, '/',
363 file_split
->predirlen
- (start
-
364 file_split
->predir
));
367 + file_split
->predirlen
- (start
- file_split
->predir
);
368 strncpy(&result
[offset
], start
, end
- start
);
369 offset
+= (int)(end
- start
);
370 result
[offset
] = '\\';
374 start
= file_split
->dir
;
375 while (file_split
->dirlen
> (start
- file_split
->dir
)) {
376 const char *end
= openssl_strnchr(start
, '/',
377 file_split
->dirlen
- (start
-
380 end
= start
+ file_split
->dirlen
- (start
- file_split
->dir
);
381 strncpy(&result
[offset
], start
, end
- start
);
382 offset
+= (int)(end
- start
);
383 result
[offset
] = '\\';
387 strncpy(&result
[offset
], file_split
->file
, file_split
->filelen
);
388 offset
+= file_split
->filelen
;
389 result
[offset
] = '\0';
393 static char *win32_merger(DSO
*dso
, const char *filespec1
,
394 const char *filespec2
)
397 struct file_st
*filespec1_split
= NULL
;
398 struct file_st
*filespec2_split
= NULL
;
400 if (!filespec1
&& !filespec2
) {
401 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_PASSED_NULL_PARAMETER
);
405 merged
= OPENSSL_malloc(strlen(filespec1
) + 1);
406 if (merged
== NULL
) {
407 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
410 strcpy(merged
, filespec1
);
411 } else if (!filespec1
) {
412 merged
= OPENSSL_malloc(strlen(filespec2
) + 1);
413 if (merged
== NULL
) {
414 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
417 strcpy(merged
, filespec2
);
419 filespec1_split
= win32_splitter(dso
, filespec1
, 0);
420 if (!filespec1_split
) {
421 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
424 filespec2_split
= win32_splitter(dso
, filespec2
, 1);
425 if (!filespec2_split
) {
426 DSOerr(DSO_F_WIN32_MERGER
, ERR_R_MALLOC_FAILURE
);
427 OPENSSL_free(filespec1_split
);
431 /* Fill in into filespec1_split */
432 if (!filespec1_split
->node
&& !filespec1_split
->device
) {
433 filespec1_split
->node
= filespec2_split
->node
;
434 filespec1_split
->nodelen
= filespec2_split
->nodelen
;
435 filespec1_split
->device
= filespec2_split
->device
;
436 filespec1_split
->devicelen
= filespec2_split
->devicelen
;
438 if (!filespec1_split
->dir
) {
439 filespec1_split
->dir
= filespec2_split
->dir
;
440 filespec1_split
->dirlen
= filespec2_split
->dirlen
;
441 } else if (filespec1_split
->dir
[0] != '\\'
442 && filespec1_split
->dir
[0] != '/') {
443 filespec1_split
->predir
= filespec2_split
->dir
;
444 filespec1_split
->predirlen
= filespec2_split
->dirlen
;
446 if (!filespec1_split
->file
) {
447 filespec1_split
->file
= filespec2_split
->file
;
448 filespec1_split
->filelen
= filespec2_split
->filelen
;
451 merged
= win32_joiner(dso
, filespec1_split
);
453 OPENSSL_free(filespec1_split
);
454 OPENSSL_free(filespec2_split
);
458 static char *win32_name_converter(DSO
*dso
, const char *filename
)
463 len
= strlen(filename
);
464 transform
= ((strstr(filename
, "/") == NULL
) &&
465 (strstr(filename
, "\\") == NULL
) &&
466 (strstr(filename
, ":") == NULL
));
468 /* We will convert this to "%s.dll" */
469 translated
= OPENSSL_malloc(len
+ 5);
471 /* We will simply duplicate filename */
472 translated
= OPENSSL_malloc(len
+ 1);
473 if (translated
== NULL
) {
474 DSOerr(DSO_F_WIN32_NAME_CONVERTER
, DSO_R_NAME_TRANSLATION_FAILED
);
478 sprintf(translated
, "%s.dll", filename
);
480 sprintf(translated
, "%s", filename
);
484 static const char *openssl_strnchr(const char *string
, int c
, size_t len
)
488 for (i
= 0, p
= string
; i
< len
&& *p
; i
++, p
++) {
495 # include <tlhelp32.h>
497 # define DLLNAME "TOOLHELP.DLL"
499 # ifdef MODULEENTRY32
500 # undef MODULEENTRY32 /* unmask the ASCII version! */
502 # define DLLNAME "KERNEL32.DLL"
505 typedef HANDLE(WINAPI
*CREATETOOLHELP32SNAPSHOT
) (DWORD
, DWORD
);
506 typedef BOOL(WINAPI
*CLOSETOOLHELP32SNAPSHOT
) (HANDLE
);
507 typedef BOOL(WINAPI
*MODULE32
) (HANDLE
, MODULEENTRY32
*);
509 static void *win32_globallookup(const char *name
)
512 HANDLE hModuleSnap
= INVALID_HANDLE_VALUE
;
514 CREATETOOLHELP32SNAPSHOT create_snap
;
515 CLOSETOOLHELP32SNAPSHOT close_snap
;
516 MODULE32 module_first
, module_next
;
522 dll
= LoadLibrary(TEXT(DLLNAME
));
524 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
528 create_snap
= (CREATETOOLHELP32SNAPSHOT
)
529 GetProcAddress(dll
, "CreateToolhelp32Snapshot");
530 if (create_snap
== NULL
) {
532 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
535 /* We take the rest for granted... */
537 close_snap
= (CLOSETOOLHELP32SNAPSHOT
)
538 GetProcAddress(dll
, "CloseToolhelp32Snapshot");
540 close_snap
= (CLOSETOOLHELP32SNAPSHOT
) CloseHandle
;
542 module_first
= (MODULE32
) GetProcAddress(dll
, "Module32First");
543 module_next
= (MODULE32
) GetProcAddress(dll
, "Module32Next");
545 hModuleSnap
= (*create_snap
) (TH32CS_SNAPMODULE
, 0);
546 if (hModuleSnap
== INVALID_HANDLE_VALUE
) {
548 DSOerr(DSO_F_WIN32_GLOBALLOOKUP
, DSO_R_UNSUPPORTED
);
552 me32
.dwSize
= sizeof(me32
);
554 if (!(*module_first
) (hModuleSnap
, &me32
)) {
555 (*close_snap
) (hModuleSnap
);
561 if ((ret
.f
= GetProcAddress(me32
.hModule
, name
))) {
562 (*close_snap
) (hModuleSnap
);
566 } while ((*module_next
) (hModuleSnap
, &me32
));
568 (*close_snap
) (hModuleSnap
);
572 #endif /* DSO_WIN32 */