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 #if defined(DSO_WIN32)
65 static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
67 WCHAR lpProcNameW[64];
70 for (i = 0; lpProcName[i] && i < 64; i++)
71 lpProcNameW[i] = (WCHAR)lpProcName[i];
76 return GetProcAddressW(hModule, lpProcNameW);
79 # undef GetProcAddress
80 # define GetProcAddress GetProcAddressA
82 static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
85 size_t len_0 = strlen(lpLibFileName) + 1, i;
88 fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
90 fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
93 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
96 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
97 if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
99 for (i = 0; i < len_0; i++)
100 fnamw[i] = (WCHAR)lpLibFileName[i];
102 return LoadLibraryW(fnamw);
106 /* Part of the hack in "win32_load" ... */
107 # define DSO_MAX_TRANSLATED_SIZE 256
109 static int win32_load(DSO *dso);
110 static int win32_unload(DSO *dso);
111 static void *win32_bind_var(DSO *dso, const char *symname);
112 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
113 static char *win32_name_converter(DSO *dso, const char *filename);
114 static char *win32_merger(DSO *dso, const char *filespec1,
115 const char *filespec2);
116 static int win32_pathbyaddr(void *addr, char *path, int sz);
117 static void *win32_globallookup(const char *name);
119 static const char *openssl_strnchr(const char *string, int c, size_t len);
121 static DSO_METHOD dso_meth_win32 = {
122 "OpenSSL 'win32' shared library method",
128 win32_name_converter,
136 DSO_METHOD *DSO_METHOD_openssl(void)
138 return &dso_meth_win32;
142 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
143 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
146 static int win32_load(DSO *dso)
148 HINSTANCE h = NULL, *p = NULL;
149 /* See applicable comments from dso_dl.c */
150 char *filename = DSO_convert_filename(dso, NULL);
152 if (filename == NULL) {
153 DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
156 h = LoadLibraryA(filename);
158 DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
159 ERR_add_error_data(3, "filename(", filename, ")");
162 p = OPENSSL_malloc(sizeof(*p));
164 DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
168 if (!sk_void_push(dso->meth_data, p)) {
169 DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
173 dso->loaded_filename = filename;
177 OPENSSL_free(filename);
184 static int win32_unload(DSO *dso)
188 DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
191 if (sk_void_num(dso->meth_data) < 1)
193 p = sk_void_pop(dso->meth_data);
195 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
198 if (!FreeLibrary(*p)) {
199 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
201 * We should push the value back onto the stack in case of a retry.
203 sk_void_push(dso->meth_data, p);
212 * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
213 * docs, there's probably a variant for variables.
215 static void *win32_bind_var(DSO *dso, const char *symname)
223 if ((dso == NULL) || (symname == NULL)) {
224 DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
227 if (sk_void_num(dso->meth_data) < 1) {
228 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
231 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
233 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
236 sym.f = GetProcAddress(*ptr, symname);
238 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
239 ERR_add_error_data(3, "symname(", symname, ")");
245 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
253 if ((dso == NULL) || (symname == NULL)) {
254 DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
257 if (sk_void_num(dso->meth_data) < 1) {
258 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
261 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
263 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
266 sym.f = GetProcAddress(*ptr, symname);
268 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
269 ERR_add_error_data(3, "symname(", symname, ")");
272 return ((DSO_FUNC_TYPE)sym.f);
288 static struct file_st *win32_splitter(DSO *dso, const char *filename,
289 int assume_last_is_dir)
291 struct file_st *result = NULL;
292 enum { IN_NODE, IN_DEVICE, IN_FILE } position;
293 const char *start = filename;
297 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
304 result = OPENSSL_zalloc(sizeof(*result));
305 if (result == NULL) {
306 DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
310 position = IN_DEVICE;
312 if ((filename[0] == '\\' && filename[1] == '\\')
313 || (filename[0] == '/' && filename[1] == '/')) {
317 result->node = start;
324 if (position != IN_DEVICE) {
325 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
329 OPENSSL_free(result);
332 result->device = start;
333 result->devicelen = (int)(filename - start);
340 if (position == IN_NODE) {
341 result->nodelen = (int)(filename - start);
345 } else if (position == IN_DEVICE) {
349 result->dirlen = (int)(filename - start);
353 result->dirlen += (int)(filename - start);
358 if (position == IN_NODE) {
359 result->nodelen = (int)(filename - start);
361 if (filename - start > 0) {
362 if (assume_last_is_dir) {
363 if (position == IN_DEVICE) {
367 result->dirlen += (int)(filename - start);
369 result->file = start;
370 result->filelen = (int)(filename - start);
382 if (!result->nodelen)
384 if (!result->devicelen)
385 result->device = NULL;
388 if (!result->filelen)
394 static char *win32_joiner(DSO *dso, const struct file_st *file_split)
396 int len = 0, offset = 0;
401 DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
404 if (file_split->node) {
405 len += 2 + file_split->nodelen; /* 2 for starting \\ */
406 if (file_split->predir || file_split->dir || file_split->file)
407 len++; /* 1 for ending \ */
408 } else if (file_split->device) {
409 len += file_split->devicelen + 1; /* 1 for ending : */
411 len += file_split->predirlen;
412 if (file_split->predir && (file_split->dir || file_split->file)) {
413 len++; /* 1 for ending \ */
415 len += file_split->dirlen;
416 if (file_split->dir && file_split->file) {
417 len++; /* 1 for ending \ */
419 len += file_split->filelen;
422 DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
426 result = OPENSSL_malloc(len + 1);
427 if (result == NULL) {
428 DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
432 if (file_split->node) {
433 strcpy(&result[offset], "\\\\");
435 strncpy(&result[offset], file_split->node, file_split->nodelen);
436 offset += file_split->nodelen;
437 if (file_split->predir || file_split->dir || file_split->file) {
438 result[offset] = '\\';
441 } else if (file_split->device) {
442 strncpy(&result[offset], file_split->device, file_split->devicelen);
443 offset += file_split->devicelen;
444 result[offset] = ':';
447 start = file_split->predir;
448 while (file_split->predirlen > (start - file_split->predir)) {
449 const char *end = openssl_strnchr(start, '/',
450 file_split->predirlen - (start -
451 file_split->predir));
454 + file_split->predirlen - (start - file_split->predir);
455 strncpy(&result[offset], start, end - start);
456 offset += (int)(end - start);
457 result[offset] = '\\';
461 start = file_split->dir;
462 while (file_split->dirlen > (start - file_split->dir)) {
463 const char *end = openssl_strnchr(start, '/',
464 file_split->dirlen - (start -
467 end = start + file_split->dirlen - (start - file_split->dir);
468 strncpy(&result[offset], start, end - start);
469 offset += (int)(end - start);
470 result[offset] = '\\';
474 strncpy(&result[offset], file_split->file, file_split->filelen);
475 offset += file_split->filelen;
476 result[offset] = '\0';
480 static char *win32_merger(DSO *dso, const char *filespec1,
481 const char *filespec2)
484 struct file_st *filespec1_split = NULL;
485 struct file_st *filespec2_split = NULL;
487 if (!filespec1 && !filespec2) {
488 DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
492 merged = OPENSSL_malloc(strlen(filespec1) + 1);
493 if (merged == NULL) {
494 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
497 strcpy(merged, filespec1);
498 } else if (!filespec1) {
499 merged = OPENSSL_malloc(strlen(filespec2) + 1);
500 if (merged == NULL) {
501 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
504 strcpy(merged, filespec2);
506 filespec1_split = win32_splitter(dso, filespec1, 0);
507 if (!filespec1_split) {
508 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
511 filespec2_split = win32_splitter(dso, filespec2, 1);
512 if (!filespec2_split) {
513 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
514 OPENSSL_free(filespec1_split);
518 /* Fill in into filespec1_split */
519 if (!filespec1_split->node && !filespec1_split->device) {
520 filespec1_split->node = filespec2_split->node;
521 filespec1_split->nodelen = filespec2_split->nodelen;
522 filespec1_split->device = filespec2_split->device;
523 filespec1_split->devicelen = filespec2_split->devicelen;
525 if (!filespec1_split->dir) {
526 filespec1_split->dir = filespec2_split->dir;
527 filespec1_split->dirlen = filespec2_split->dirlen;
528 } else if (filespec1_split->dir[0] != '\\'
529 && filespec1_split->dir[0] != '/') {
530 filespec1_split->predir = filespec2_split->dir;
531 filespec1_split->predirlen = filespec2_split->dirlen;
533 if (!filespec1_split->file) {
534 filespec1_split->file = filespec2_split->file;
535 filespec1_split->filelen = filespec2_split->filelen;
538 merged = win32_joiner(dso, filespec1_split);
540 OPENSSL_free(filespec1_split);
541 OPENSSL_free(filespec2_split);
545 static char *win32_name_converter(DSO *dso, const char *filename)
550 len = strlen(filename);
551 transform = ((strstr(filename, "/") == NULL) &&
552 (strstr(filename, "\\") == NULL) &&
553 (strstr(filename, ":") == NULL));
555 /* We will convert this to "%s.dll" */
556 translated = OPENSSL_malloc(len + 5);
558 /* We will simply duplicate filename */
559 translated = OPENSSL_malloc(len + 1);
560 if (translated == NULL) {
561 DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
565 sprintf(translated, "%s.dll", filename);
567 sprintf(translated, "%s", filename);
571 static const char *openssl_strnchr(const char *string, int c, size_t len)
575 for (i = 0, p = string; i < len && *p; i++, p++) {
582 # include <tlhelp32.h>
584 # define DLLNAME "TOOLHELP.DLL"
586 # ifdef MODULEENTRY32
587 # undef MODULEENTRY32 /* unmask the ASCII version! */
589 # define DLLNAME "KERNEL32.DLL"
592 typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
593 typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
594 typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
596 static int win32_pathbyaddr(void *addr, char *path, int sz)
599 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
601 CREATETOOLHELP32SNAPSHOT create_snap;
602 CLOSETOOLHELP32SNAPSHOT close_snap;
603 MODULE32 module_first, module_next;
607 int (*f) (void *, char *, int);
615 dll = LoadLibrary(TEXT(DLLNAME));
617 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
621 create_snap = (CREATETOOLHELP32SNAPSHOT)
622 GetProcAddress(dll, "CreateToolhelp32Snapshot");
623 if (create_snap == NULL) {
625 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
628 /* We take the rest for granted... */
630 close_snap = (CLOSETOOLHELP32SNAPSHOT)
631 GetProcAddress(dll, "CloseToolhelp32Snapshot");
633 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
635 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
636 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
638 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
639 if (hModuleSnap == INVALID_HANDLE_VALUE) {
641 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_UNSUPPORTED);
645 me32.dwSize = sizeof(me32);
647 if (!(*module_first) (hModuleSnap, &me32)) {
648 (*close_snap) (hModuleSnap);
650 DSOerr(DSO_F_WIN32_PATHBYADDR, DSO_R_FAILURE);
655 if ((BYTE *) addr >= me32.modBaseAddr &&
656 (BYTE *) addr < me32.modBaseAddr + me32.modBaseSize) {
657 (*close_snap) (hModuleSnap);
660 # if _WIN32_WCE >= 101
661 return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
662 path, sz, NULL, NULL);
665 int i, len = (int)wcslen(me32.szExePath);
670 for (i = 0; i < len; i++)
671 path[i] = (char)me32.szExePath[i];
678 int len = (int)strlen(me32.szExePath);
683 memcpy(path, me32.szExePath, len);
689 } while ((*module_next) (hModuleSnap, &me32));
691 (*close_snap) (hModuleSnap);
696 static void *win32_globallookup(const char *name)
699 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
701 CREATETOOLHELP32SNAPSHOT create_snap;
702 CLOSETOOLHELP32SNAPSHOT close_snap;
703 MODULE32 module_first, module_next;
709 dll = LoadLibrary(TEXT(DLLNAME));
711 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
715 create_snap = (CREATETOOLHELP32SNAPSHOT)
716 GetProcAddress(dll, "CreateToolhelp32Snapshot");
717 if (create_snap == NULL) {
719 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
722 /* We take the rest for granted... */
724 close_snap = (CLOSETOOLHELP32SNAPSHOT)
725 GetProcAddress(dll, "CloseToolhelp32Snapshot");
727 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
729 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
730 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
732 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
733 if (hModuleSnap == INVALID_HANDLE_VALUE) {
735 DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
739 me32.dwSize = sizeof(me32);
741 if (!(*module_first) (hModuleSnap, &me32)) {
742 (*close_snap) (hModuleSnap);
748 if ((ret.f = GetProcAddress(me32.hModule, name))) {
749 (*close_snap) (hModuleSnap);
753 } while ((*module_next) (hModuleSnap, &me32));
755 (*close_snap) (hModuleSnap);
759 #endif /* DSO_WIN32 */