2 * Copyright 2000-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include "internal/e_os.h"
11 #include "dso_local.h"
13 #if defined(DSO_WIN32)
17 static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
19 WCHAR lpProcNameW[64];
22 for (i = 0; lpProcName[i] && i < 64; i++)
23 lpProcNameW[i] = (WCHAR)lpProcName[i];
28 return GetProcAddressW(hModule, lpProcNameW);
31 # undef GetProcAddress
32 # define GetProcAddress GetProcAddressA
34 static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
37 size_t len_0 = strlen(lpLibFileName) + 1, i;
40 fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
42 fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
45 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
48 # if defined(_WIN32_WCE) && _WIN32_WCE>=101
49 if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
51 for (i = 0; i < len_0; i++)
52 fnamw[i] = (WCHAR)lpLibFileName[i];
54 return LoadLibraryW(fnamw);
58 /* Part of the hack in "win32_load" ... */
59 # define DSO_MAX_TRANSLATED_SIZE 256
61 static int win32_load(DSO *dso);
62 static int win32_unload(DSO *dso);
63 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
64 static char *win32_name_converter(DSO *dso, const char *filename);
65 static char *win32_merger(DSO *dso, const char *filespec1,
66 const char *filespec2);
67 static int win32_pathbyaddr(void *addr, char *path, int sz);
68 static void *win32_globallookup(const char *name);
70 static const char *openssl_strnchr(const char *string, int c, size_t len);
72 static DSO_METHOD dso_meth_win32 = {
73 "OpenSSL 'win32' shared library method",
82 win32_pathbyaddr, /* pathbyaddr */
86 DSO_METHOD *DSO_METHOD_openssl(void)
88 return &dso_meth_win32;
92 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
93 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
96 static int win32_load(DSO *dso)
98 HINSTANCE h = NULL, *p = NULL;
99 /* See applicable comments from dso_dl.c */
100 char *filename = DSO_convert_filename(dso, NULL);
102 if (filename == NULL) {
103 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
106 h = LoadLibraryA(filename);
108 ERR_raise_data(ERR_LIB_DSO, DSO_R_LOAD_FAILED,
109 "filename(%s)", filename);
112 p = OPENSSL_malloc(sizeof(*p));
116 if (!sk_void_push(dso->meth_data, p)) {
117 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
121 dso->loaded_filename = filename;
125 OPENSSL_free(filename);
132 static int win32_unload(DSO *dso)
136 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
139 if (sk_void_num(dso->meth_data) < 1)
141 p = sk_void_pop(dso->meth_data);
143 ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
146 if (!FreeLibrary(*p)) {
147 ERR_raise(ERR_LIB_DSO, DSO_R_UNLOAD_FAILED);
149 * We should push the value back onto the stack in case of a retry.
151 sk_void_push(dso->meth_data, p);
159 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
167 if ((dso == NULL) || (symname == NULL)) {
168 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
171 if (sk_void_num(dso->meth_data) < 1) {
172 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
175 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
177 ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
180 sym.f = GetProcAddress(*ptr, symname);
182 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE, "symname(%s)", 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 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
214 result = OPENSSL_zalloc(sizeof(*result));
218 position = IN_DEVICE;
220 if ((filename[0] == '\\' && filename[1] == '\\')
221 || (filename[0] == '/' && filename[1] == '/')) {
225 result->node = start;
232 if (position != IN_DEVICE) {
233 ERR_raise(ERR_LIB_DSO, DSO_R_INCORRECT_FILE_SYNTAX);
234 OPENSSL_free(result);
237 result->device = start;
238 result->devicelen = (int)(filename - start);
245 if (position == IN_NODE) {
246 result->nodelen = (int)(filename - start);
250 } else if (position == IN_DEVICE) {
254 result->dirlen = (int)(filename - start);
258 result->dirlen += (int)(filename - start);
263 if (position == IN_NODE) {
264 result->nodelen = (int)(filename - start);
266 if (filename - start > 0) {
267 if (assume_last_is_dir) {
268 if (position == IN_DEVICE) {
272 result->dirlen += (int)(filename - start);
274 result->file = start;
275 result->filelen = (int)(filename - start);
287 if (!result->nodelen)
289 if (!result->devicelen)
290 result->device = NULL;
293 if (!result->filelen)
299 static char *win32_joiner(DSO *dso, const struct file_st *file_split)
301 int len = 0, offset = 0;
306 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
309 if (file_split->node) {
310 len += 2 + file_split->nodelen; /* 2 for starting \\ */
311 if (file_split->predir || file_split->dir || file_split->file)
312 len++; /* 1 for ending \ */
313 } else if (file_split->device) {
314 len += file_split->devicelen + 1; /* 1 for ending : */
316 len += file_split->predirlen;
317 if (file_split->predir && (file_split->dir || file_split->file)) {
318 len++; /* 1 for ending \ */
320 len += file_split->dirlen;
321 if (file_split->dir && file_split->file) {
322 len++; /* 1 for ending \ */
324 len += file_split->filelen;
327 ERR_raise(ERR_LIB_DSO, DSO_R_EMPTY_FILE_STRUCTURE);
331 result = OPENSSL_malloc(len + 1);
335 if (file_split->node) {
336 strcpy(&result[offset], "\\\\");
338 strncpy(&result[offset], file_split->node, file_split->nodelen);
339 offset += file_split->nodelen;
340 if (file_split->predir || file_split->dir || file_split->file) {
341 result[offset] = '\\';
344 } else if (file_split->device) {
345 strncpy(&result[offset], file_split->device, file_split->devicelen);
346 offset += file_split->devicelen;
347 result[offset] = ':';
350 start = file_split->predir;
351 while (file_split->predirlen > (start - file_split->predir)) {
352 const char *end = openssl_strnchr(start, '/',
353 file_split->predirlen - (start -
354 file_split->predir));
357 + file_split->predirlen - (start - file_split->predir);
358 strncpy(&result[offset], start, end - start);
359 offset += (int)(end - start);
360 result[offset] = '\\';
364 start = file_split->dir;
365 while (file_split->dirlen > (start - file_split->dir)) {
366 const char *end = openssl_strnchr(start, '/',
367 file_split->dirlen - (start -
370 end = start + file_split->dirlen - (start - file_split->dir);
371 strncpy(&result[offset], start, end - start);
372 offset += (int)(end - start);
373 result[offset] = '\\';
377 strncpy(&result[offset], file_split->file, file_split->filelen);
378 offset += file_split->filelen;
379 result[offset] = '\0';
383 static char *win32_merger(DSO *dso, const char *filespec1,
384 const char *filespec2)
387 struct file_st *filespec1_split = NULL;
388 struct file_st *filespec2_split = NULL;
390 if (!filespec1 && !filespec2) {
391 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
395 merged = OPENSSL_strdup(filespec1);
398 } else if (!filespec1) {
399 merged = OPENSSL_strdup(filespec2);
403 filespec1_split = win32_splitter(dso, filespec1, 0);
404 if (!filespec1_split) {
405 ERR_raise(ERR_LIB_DSO, ERR_R_DSO_LIB);
408 filespec2_split = win32_splitter(dso, filespec2, 1);
409 if (!filespec2_split) {
410 ERR_raise(ERR_LIB_DSO, ERR_R_DSO_LIB);
411 OPENSSL_free(filespec1_split);
415 /* Fill in into filespec1_split */
416 if (!filespec1_split->node && !filespec1_split->device) {
417 filespec1_split->node = filespec2_split->node;
418 filespec1_split->nodelen = filespec2_split->nodelen;
419 filespec1_split->device = filespec2_split->device;
420 filespec1_split->devicelen = filespec2_split->devicelen;
422 if (!filespec1_split->dir) {
423 filespec1_split->dir = filespec2_split->dir;
424 filespec1_split->dirlen = filespec2_split->dirlen;
425 } else if (filespec1_split->dir[0] != '\\'
426 && filespec1_split->dir[0] != '/') {
427 filespec1_split->predir = filespec2_split->dir;
428 filespec1_split->predirlen = filespec2_split->dirlen;
430 if (!filespec1_split->file) {
431 filespec1_split->file = filespec2_split->file;
432 filespec1_split->filelen = filespec2_split->filelen;
435 merged = win32_joiner(dso, filespec1_split);
437 OPENSSL_free(filespec1_split);
438 OPENSSL_free(filespec2_split);
442 static char *win32_name_converter(DSO *dso, const char *filename)
447 len = strlen(filename);
448 transform = ((strstr(filename, "/") == NULL) &&
449 (strstr(filename, "\\") == NULL) &&
450 (strstr(filename, ":") == NULL));
452 /* We will convert this to "%s.dll" */
453 translated = OPENSSL_malloc(len + 5);
455 /* We will simply duplicate filename */
456 translated = OPENSSL_malloc(len + 1);
457 if (translated == NULL) {
458 ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED);
462 sprintf(translated, "%s.dll", filename);
464 sprintf(translated, "%s", filename);
468 static const char *openssl_strnchr(const char *string, int c, size_t len)
472 for (i = 0, p = string; i < len && *p; i++, p++) {
479 # include <tlhelp32.h>
481 # define DLLNAME "TOOLHELP.DLL"
483 # ifdef MODULEENTRY32
484 # undef MODULEENTRY32 /* unmask the ASCII version! */
486 # define DLLNAME "KERNEL32.DLL"
489 typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
490 typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
491 typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
493 static int win32_pathbyaddr(void *addr, char *path, int sz)
496 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
498 CREATETOOLHELP32SNAPSHOT create_snap;
499 CLOSETOOLHELP32SNAPSHOT close_snap;
500 MODULE32 module_first, module_next;
504 int (*f) (void *, char *, int);
512 dll = LoadLibrary(TEXT(DLLNAME));
514 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
518 create_snap = (CREATETOOLHELP32SNAPSHOT)
519 GetProcAddress(dll, "CreateToolhelp32Snapshot");
520 if (create_snap == NULL) {
522 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
525 /* We take the rest for granted... */
527 close_snap = (CLOSETOOLHELP32SNAPSHOT)
528 GetProcAddress(dll, "CloseToolhelp32Snapshot");
530 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
532 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
533 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
536 * Take a snapshot of current process which includes
537 * list of all involved modules.
539 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
540 if (hModuleSnap == INVALID_HANDLE_VALUE) {
542 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
546 me32.dwSize = sizeof(me32);
548 if (!(*module_first) (hModuleSnap, &me32)) {
549 (*close_snap) (hModuleSnap);
551 ERR_raise(ERR_LIB_DSO, DSO_R_FAILURE);
555 /* Enumerate the modules to find one which includes me. */
557 if ((size_t) addr >= (size_t) me32.modBaseAddr &&
558 (size_t) addr < (size_t) (me32.modBaseAddr + me32.modBaseSize)) {
559 (*close_snap) (hModuleSnap);
562 # if _WIN32_WCE >= 101
563 return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
564 path, sz, NULL, NULL);
567 int i, len = (int)wcslen(me32.szExePath);
572 for (i = 0; i < len; i++)
573 path[i] = (char)me32.szExePath[i];
580 int len = (int)strlen(me32.szExePath);
585 memcpy(path, me32.szExePath, len);
591 } while ((*module_next) (hModuleSnap, &me32));
593 (*close_snap) (hModuleSnap);
598 static void *win32_globallookup(const char *name)
601 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
603 CREATETOOLHELP32SNAPSHOT create_snap;
604 CLOSETOOLHELP32SNAPSHOT close_snap;
605 MODULE32 module_first, module_next;
611 dll = LoadLibrary(TEXT(DLLNAME));
613 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
617 create_snap = (CREATETOOLHELP32SNAPSHOT)
618 GetProcAddress(dll, "CreateToolhelp32Snapshot");
619 if (create_snap == NULL) {
621 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
624 /* We take the rest for granted... */
626 close_snap = (CLOSETOOLHELP32SNAPSHOT)
627 GetProcAddress(dll, "CloseToolhelp32Snapshot");
629 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
631 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
632 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
634 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
635 if (hModuleSnap == INVALID_HANDLE_VALUE) {
637 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
641 me32.dwSize = sizeof(me32);
643 if (!(*module_first) (hModuleSnap, &me32)) {
644 (*close_snap) (hModuleSnap);
650 if ((ret.f = GetProcAddress(me32.hModule, name))) {
651 (*close_snap) (hModuleSnap);
655 } while ((*module_next) (hModuleSnap, &me32));
657 (*close_snap) (hModuleSnap);
661 #endif /* DSO_WIN32 */