2 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
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).
61 #include "internal/cryptlib.h"
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];
84 return GetProcAddressW(hModule, lpProcNameW);
87 # undef GetProcAddress
88 # define GetProcAddress GetProcAddressA
90 static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
93 size_t len_0 = strlen(lpLibFileName) + 1, i;
96 fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
98 fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
101 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
104 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
105 if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
107 for (i = 0; i < len_0; i++)
108 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);
121 static char *win32_name_converter(DSO *dso, const char *filename);
122 static char *win32_merger(DSO *dso, const char *filespec1,
123 const char *filespec2);
124 static int win32_pathbyaddr(void *addr, char *path, int sz);
125 static void *win32_globallookup(const char *name);
127 static const char *openssl_strnchr(const char *string, int c, size_t len);
129 static DSO_METHOD dso_meth_win32 = {
130 "OpenSSL 'win32' shared library method",
136 win32_name_converter,
144 DSO_METHOD *DSO_METHOD_win32(void)
146 return (&dso_meth_win32);
150 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
151 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
154 static int win32_load(DSO *dso)
156 HINSTANCE h = NULL, *p = NULL;
157 /* See applicable comments from dso_dl.c */
158 char *filename = DSO_convert_filename(dso, NULL);
160 if (filename == NULL) {
161 DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
164 h = LoadLibraryA(filename);
166 DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
167 ERR_add_error_data(3, "filename(", filename, ")");
170 p = OPENSSL_malloc(sizeof(*p));
172 DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
176 if (!sk_void_push(dso->meth_data, p)) {
177 DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
181 dso->loaded_filename = filename;
185 OPENSSL_free(filename);
192 static int win32_unload(DSO *dso)
196 DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
199 if (sk_void_num(dso->meth_data) < 1)
201 p = sk_void_pop(dso->meth_data);
203 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
206 if (!FreeLibrary(*p)) {
207 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
209 * We should push the value back onto the stack in case of a retry.
211 sk_void_push(dso->meth_data, p);
220 * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
221 * docs, there's probably a variant for variables.
223 static void *win32_bind_var(DSO *dso, const char *symname)
231 if ((dso == NULL) || (symname == NULL)) {
232 DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
235 if (sk_void_num(dso->meth_data) < 1) {
236 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
239 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
241 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
244 sym.f = GetProcAddress(*ptr, symname);
246 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
247 ERR_add_error_data(3, "symname(", symname, ")");
253 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
261 if ((dso == NULL) || (symname == NULL)) {
262 DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
265 if (sk_void_num(dso->meth_data) < 1) {
266 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
269 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
271 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
274 sym.f = GetProcAddress(*ptr, symname);
276 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
277 ERR_add_error_data(3, "symname(", symname, ")");
280 return ((DSO_FUNC_TYPE)sym.f);
296 static struct file_st *win32_splitter(DSO *dso, const char *filename,
297 int assume_last_is_dir)
299 struct file_st *result = NULL;
300 enum { IN_NODE, IN_DEVICE, IN_FILE } position;
301 const char *start = filename;
305 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
312 result = OPENSSL_zalloc(sizeof(*result));
313 if (result == NULL) {
314 DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
318 position = IN_DEVICE;
320 if ((filename[0] == '\\' && filename[1] == '\\')
321 || (filename[0] == '/' && filename[1] == '/')) {
325 result->node = start;
332 if (position != IN_DEVICE) {
333 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
337 OPENSSL_free(result);
340 result->device = start;
341 result->devicelen = (int)(filename - start);
348 if (position == IN_NODE) {
349 result->nodelen = (int)(filename - start);
353 } else if (position == IN_DEVICE) {
357 result->dirlen = (int)(filename - start);
361 result->dirlen += (int)(filename - start);
366 if (position == IN_NODE) {
367 result->nodelen = (int)(filename - start);
369 if (filename - start > 0) {
370 if (assume_last_is_dir) {
371 if (position == IN_DEVICE) {
375 result->dirlen += (int)(filename - start);
377 result->file = start;
378 result->filelen = (int)(filename - start);
390 if (!result->nodelen)
392 if (!result->devicelen)
393 result->device = NULL;
396 if (!result->filelen)
402 static char *win32_joiner(DSO *dso, const struct file_st *file_split)
404 int len = 0, offset = 0;
409 DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
412 if (file_split->node) {
413 len += 2 + file_split->nodelen; /* 2 for starting \\ */
414 if (file_split->predir || file_split->dir || file_split->file)
415 len++; /* 1 for ending \ */
416 } else if (file_split->device) {
417 len += file_split->devicelen + 1; /* 1 for ending : */
419 len += file_split->predirlen;
420 if (file_split->predir && (file_split->dir || file_split->file)) {
421 len++; /* 1 for ending \ */
423 len += file_split->dirlen;
424 if (file_split->dir && file_split->file) {
425 len++; /* 1 for ending \ */
427 len += file_split->filelen;
430 DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
434 result = OPENSSL_malloc(len + 1);
435 if (result == NULL) {
436 DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
440 if (file_split->node) {
441 strcpy(&result[offset], "\\\\");
443 strncpy(&result[offset], file_split->node, file_split->nodelen);
444 offset += file_split->nodelen;
445 if (file_split->predir || file_split->dir || file_split->file) {
446 result[offset] = '\\';
449 } else if (file_split->device) {
450 strncpy(&result[offset], file_split->device, file_split->devicelen);
451 offset += file_split->devicelen;
452 result[offset] = ':';
455 start = file_split->predir;
456 while (file_split->predirlen > (start - file_split->predir)) {
457 const char *end = openssl_strnchr(start, '/',
458 file_split->predirlen - (start -
459 file_split->predir));
462 + file_split->predirlen - (start - file_split->predir);
463 strncpy(&result[offset], start, end - start);
464 offset += (int)(end - start);
465 result[offset] = '\\';
469 start = file_split->dir;
470 while (file_split->dirlen > (start - file_split->dir)) {
471 const char *end = openssl_strnchr(start, '/',
472 file_split->dirlen - (start -
475 end = start + file_split->dirlen - (start - file_split->dir);
476 strncpy(&result[offset], start, end - start);
477 offset += (int)(end - start);
478 result[offset] = '\\';
482 strncpy(&result[offset], file_split->file, file_split->filelen);
483 offset += file_split->filelen;
484 result[offset] = '\0';
488 static char *win32_merger(DSO *dso, const char *filespec1,
489 const char *filespec2)
492 struct file_st *filespec1_split = NULL;
493 struct file_st *filespec2_split = NULL;
495 if (!filespec1 && !filespec2) {
496 DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
500 merged = OPENSSL_malloc(strlen(filespec1) + 1);
501 if (merged == NULL) {
502 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
505 strcpy(merged, filespec1);
506 } else if (!filespec1) {
507 merged = OPENSSL_malloc(strlen(filespec2) + 1);
508 if (merged == NULL) {
509 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
512 strcpy(merged, filespec2);
514 filespec1_split = win32_splitter(dso, filespec1, 0);
515 if (!filespec1_split) {
516 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
519 filespec2_split = win32_splitter(dso, filespec2, 1);
520 if (!filespec2_split) {
521 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
522 OPENSSL_free(filespec1_split);
526 /* Fill in into filespec1_split */
527 if (!filespec1_split->node && !filespec1_split->device) {
528 filespec1_split->node = filespec2_split->node;
529 filespec1_split->nodelen = filespec2_split->nodelen;
530 filespec1_split->device = filespec2_split->device;
531 filespec1_split->devicelen = filespec2_split->devicelen;
533 if (!filespec1_split->dir) {
534 filespec1_split->dir = filespec2_split->dir;
535 filespec1_split->dirlen = filespec2_split->dirlen;
536 } else if (filespec1_split->dir[0] != '\\'
537 && filespec1_split->dir[0] != '/') {
538 filespec1_split->predir = filespec2_split->dir;
539 filespec1_split->predirlen = filespec2_split->dirlen;
541 if (!filespec1_split->file) {
542 filespec1_split->file = filespec2_split->file;
543 filespec1_split->filelen = filespec2_split->filelen;
546 merged = win32_joiner(dso, filespec1_split);
548 OPENSSL_free(filespec1_split);
549 OPENSSL_free(filespec2_split);
553 static char *win32_name_converter(DSO *dso, const char *filename)
558 len = strlen(filename);
559 transform = ((strstr(filename, "/") == NULL) &&
560 (strstr(filename, "\\") == NULL) &&
561 (strstr(filename, ":") == NULL));
563 /* We will convert this to "%s.dll" */
564 translated = OPENSSL_malloc(len + 5);
566 /* We will simply duplicate filename */
567 translated = OPENSSL_malloc(len + 1);
568 if (translated == NULL) {
569 DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
573 sprintf(translated, "%s.dll", filename);
575 sprintf(translated, "%s", filename);
579 static const char *openssl_strnchr(const char *string, int c, size_t len)
583 for (i = 0, p = string; i < len && *p; i++, p++) {
590 # include <tlhelp32.h>
592 # define DLLNAME "TOOLHELP.DLL"
594 # ifdef MODULEENTRY32
595 # undef MODULEENTRY32 /* unmask the ASCII version! */
597 # define DLLNAME "KERNEL32.DLL"
600 typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
601 typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
602 typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
604 static int win32_pathbyaddr(void *addr, char *path, int sz)
607 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
609 CREATETOOLHELP32SNAPSHOT create_snap;
610 CLOSETOOLHELP32SNAPSHOT close_snap;
611 MODULE32 module_first, module_next;
615 int (*f) (void *, char *, int);
623 dll = LoadLibrary(TEXT(DLLNAME));
625 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
629 create_snap = (CREATETOOLHELP32SNAPSHOT)
630 GetProcAddress(dll, "CreateToolhelp32Snapshot");
631 if (create_snap == NULL) {
633 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
636 /* We take the rest for granted... */
638 close_snap = (CLOSETOOLHELP32SNAPSHOT)
639 GetProcAddress(dll, "CloseToolhelp32Snapshot");
641 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
643 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
644 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
646 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
647 if (hModuleSnap == INVALID_HANDLE_VALUE) {
649 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
653 me32.dwSize = sizeof(me32);
655 if (!(*module_first) (hModuleSnap, &me32)) {
656 (*close_snap) (hModuleSnap);
658 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_FAILURE);
663 if ((BYTE *) addr >= me32.modBaseAddr &&
664 (BYTE *) addr < me32.modBaseAddr + me32.modBaseSize) {
665 (*close_snap) (hModuleSnap);
668 # if _WIN32_WCE >= 101
669 return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
670 path, sz, NULL, NULL);
673 int i, len = (int)wcslen(me32.szExePath);
678 for (i = 0; i < len; i++)
679 path[i] = (char)me32.szExePath[i];
686 int len = (int)strlen(me32.szExePath);
691 memcpy(path, me32.szExePath, len);
697 } while ((*module_next) (hModuleSnap, &me32));
699 (*close_snap) (hModuleSnap);
704 static void *win32_globallookup(const char *name)
707 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
709 CREATETOOLHELP32SNAPSHOT create_snap;
710 CLOSETOOLHELP32SNAPSHOT close_snap;
711 MODULE32 module_first, module_next;
717 dll = LoadLibrary(TEXT(DLLNAME));
719 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
723 create_snap = (CREATETOOLHELP32SNAPSHOT)
724 GetProcAddress(dll, "CreateToolhelp32Snapshot");
725 if (create_snap == NULL) {
727 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
730 /* We take the rest for granted... */
732 close_snap = (CLOSETOOLHELP32SNAPSHOT)
733 GetProcAddress(dll, "CloseToolhelp32Snapshot");
735 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
737 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
738 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
740 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
741 if (hModuleSnap == INVALID_HANDLE_VALUE) {
743 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
747 me32.dwSize = sizeof(me32);
749 if (!(*module_first) (hModuleSnap, &me32)) {
750 (*close_snap) (hModuleSnap);
756 if ((ret.f = GetProcAddress(me32.hModule, name))) {
757 (*close_snap) (hModuleSnap);
761 } while ((*module_next) (hModuleSnap, &me32));
763 (*close_snap) (hModuleSnap);
767 #endif /* DSO_WIN32 */