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));
114 ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
118 if (!sk_void_push(dso->meth_data, p)) {
119 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
123 dso->loaded_filename = filename;
127 OPENSSL_free(filename);
134 static int win32_unload(DSO *dso)
138 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
141 if (sk_void_num(dso->meth_data) < 1)
143 p = sk_void_pop(dso->meth_data);
145 ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
148 if (!FreeLibrary(*p)) {
149 ERR_raise(ERR_LIB_DSO, DSO_R_UNLOAD_FAILED);
151 * We should push the value back onto the stack in case of a retry.
153 sk_void_push(dso->meth_data, p);
161 static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
169 if ((dso == NULL) || (symname == NULL)) {
170 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
173 if (sk_void_num(dso->meth_data) < 1) {
174 ERR_raise(ERR_LIB_DSO, DSO_R_STACK_ERROR);
177 ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
179 ERR_raise(ERR_LIB_DSO, DSO_R_NULL_HANDLE);
182 sym.f = GetProcAddress(*ptr, symname);
184 ERR_raise_data(ERR_LIB_DSO, DSO_R_SYM_FAILURE, "symname(%s)", symname);
187 return (DSO_FUNC_TYPE)sym.f;
203 static struct file_st *win32_splitter(DSO *dso, const char *filename,
204 int assume_last_is_dir)
206 struct file_st *result = NULL;
207 enum { IN_NODE, IN_DEVICE, IN_FILE } position;
208 const char *start = filename;
212 ERR_raise(ERR_LIB_DSO, DSO_R_NO_FILENAME);
216 result = OPENSSL_zalloc(sizeof(*result));
217 if (result == NULL) {
218 ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
222 position = IN_DEVICE;
224 if ((filename[0] == '\\' && filename[1] == '\\')
225 || (filename[0] == '/' && filename[1] == '/')) {
229 result->node = start;
236 if (position != IN_DEVICE) {
237 ERR_raise(ERR_LIB_DSO, DSO_R_INCORRECT_FILE_SYNTAX);
238 OPENSSL_free(result);
241 result->device = start;
242 result->devicelen = (int)(filename - start);
249 if (position == IN_NODE) {
250 result->nodelen = (int)(filename - start);
254 } else if (position == IN_DEVICE) {
258 result->dirlen = (int)(filename - start);
262 result->dirlen += (int)(filename - start);
267 if (position == IN_NODE) {
268 result->nodelen = (int)(filename - start);
270 if (filename - start > 0) {
271 if (assume_last_is_dir) {
272 if (position == IN_DEVICE) {
276 result->dirlen += (int)(filename - start);
278 result->file = start;
279 result->filelen = (int)(filename - start);
291 if (!result->nodelen)
293 if (!result->devicelen)
294 result->device = NULL;
297 if (!result->filelen)
303 static char *win32_joiner(DSO *dso, const struct file_st *file_split)
305 int len = 0, offset = 0;
310 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
313 if (file_split->node) {
314 len += 2 + file_split->nodelen; /* 2 for starting \\ */
315 if (file_split->predir || file_split->dir || file_split->file)
316 len++; /* 1 for ending \ */
317 } else if (file_split->device) {
318 len += file_split->devicelen + 1; /* 1 for ending : */
320 len += file_split->predirlen;
321 if (file_split->predir && (file_split->dir || file_split->file)) {
322 len++; /* 1 for ending \ */
324 len += file_split->dirlen;
325 if (file_split->dir && file_split->file) {
326 len++; /* 1 for ending \ */
328 len += file_split->filelen;
331 ERR_raise(ERR_LIB_DSO, DSO_R_EMPTY_FILE_STRUCTURE);
335 result = OPENSSL_malloc(len + 1);
336 if (result == NULL) {
337 ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
341 if (file_split->node) {
342 strcpy(&result[offset], "\\\\");
344 strncpy(&result[offset], file_split->node, file_split->nodelen);
345 offset += file_split->nodelen;
346 if (file_split->predir || file_split->dir || file_split->file) {
347 result[offset] = '\\';
350 } else if (file_split->device) {
351 strncpy(&result[offset], file_split->device, file_split->devicelen);
352 offset += file_split->devicelen;
353 result[offset] = ':';
356 start = file_split->predir;
357 while (file_split->predirlen > (start - file_split->predir)) {
358 const char *end = openssl_strnchr(start, '/',
359 file_split->predirlen - (start -
360 file_split->predir));
363 + file_split->predirlen - (start - file_split->predir);
364 strncpy(&result[offset], start, end - start);
365 offset += (int)(end - start);
366 result[offset] = '\\';
370 start = file_split->dir;
371 while (file_split->dirlen > (start - file_split->dir)) {
372 const char *end = openssl_strnchr(start, '/',
373 file_split->dirlen - (start -
376 end = start + file_split->dirlen - (start - file_split->dir);
377 strncpy(&result[offset], start, end - start);
378 offset += (int)(end - start);
379 result[offset] = '\\';
383 strncpy(&result[offset], file_split->file, file_split->filelen);
384 offset += file_split->filelen;
385 result[offset] = '\0';
389 static char *win32_merger(DSO *dso, const char *filespec1,
390 const char *filespec2)
393 struct file_st *filespec1_split = NULL;
394 struct file_st *filespec2_split = NULL;
396 if (!filespec1 && !filespec2) {
397 ERR_raise(ERR_LIB_DSO, ERR_R_PASSED_NULL_PARAMETER);
401 merged = OPENSSL_strdup(filespec1);
402 if (merged == NULL) {
403 ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
406 } else if (!filespec1) {
407 merged = OPENSSL_strdup(filespec2);
408 if (merged == NULL) {
409 ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
413 filespec1_split = win32_splitter(dso, filespec1, 0);
414 if (!filespec1_split) {
415 ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
418 filespec2_split = win32_splitter(dso, filespec2, 1);
419 if (!filespec2_split) {
420 ERR_raise(ERR_LIB_DSO, ERR_R_MALLOC_FAILURE);
421 OPENSSL_free(filespec1_split);
425 /* Fill in into filespec1_split */
426 if (!filespec1_split->node && !filespec1_split->device) {
427 filespec1_split->node = filespec2_split->node;
428 filespec1_split->nodelen = filespec2_split->nodelen;
429 filespec1_split->device = filespec2_split->device;
430 filespec1_split->devicelen = filespec2_split->devicelen;
432 if (!filespec1_split->dir) {
433 filespec1_split->dir = filespec2_split->dir;
434 filespec1_split->dirlen = filespec2_split->dirlen;
435 } else if (filespec1_split->dir[0] != '\\'
436 && filespec1_split->dir[0] != '/') {
437 filespec1_split->predir = filespec2_split->dir;
438 filespec1_split->predirlen = filespec2_split->dirlen;
440 if (!filespec1_split->file) {
441 filespec1_split->file = filespec2_split->file;
442 filespec1_split->filelen = filespec2_split->filelen;
445 merged = win32_joiner(dso, filespec1_split);
447 OPENSSL_free(filespec1_split);
448 OPENSSL_free(filespec2_split);
452 static char *win32_name_converter(DSO *dso, const char *filename)
457 len = strlen(filename);
458 transform = ((strstr(filename, "/") == NULL) &&
459 (strstr(filename, "\\") == NULL) &&
460 (strstr(filename, ":") == NULL));
462 /* We will convert this to "%s.dll" */
463 translated = OPENSSL_malloc(len + 5);
465 /* We will simply duplicate filename */
466 translated = OPENSSL_malloc(len + 1);
467 if (translated == NULL) {
468 ERR_raise(ERR_LIB_DSO, DSO_R_NAME_TRANSLATION_FAILED);
472 sprintf(translated, "%s.dll", filename);
474 sprintf(translated, "%s", filename);
478 static const char *openssl_strnchr(const char *string, int c, size_t len)
482 for (i = 0, p = string; i < len && *p; i++, p++) {
489 # include <tlhelp32.h>
491 # define DLLNAME "TOOLHELP.DLL"
493 # ifdef MODULEENTRY32
494 # undef MODULEENTRY32 /* unmask the ASCII version! */
496 # define DLLNAME "KERNEL32.DLL"
499 typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
500 typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
501 typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
503 static int win32_pathbyaddr(void *addr, char *path, int sz)
506 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
508 CREATETOOLHELP32SNAPSHOT create_snap;
509 CLOSETOOLHELP32SNAPSHOT close_snap;
510 MODULE32 module_first, module_next;
514 int (*f) (void *, char *, int);
522 dll = LoadLibrary(TEXT(DLLNAME));
524 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
528 create_snap = (CREATETOOLHELP32SNAPSHOT)
529 GetProcAddress(dll, "CreateToolhelp32Snapshot");
530 if (create_snap == NULL) {
532 ERR_raise(ERR_LIB_DSO, 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");
546 * Take a snapshot of current process which includes
547 * list of all involved modules.
549 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
550 if (hModuleSnap == INVALID_HANDLE_VALUE) {
552 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
556 me32.dwSize = sizeof(me32);
558 if (!(*module_first) (hModuleSnap, &me32)) {
559 (*close_snap) (hModuleSnap);
561 ERR_raise(ERR_LIB_DSO, DSO_R_FAILURE);
565 /* Enumerate the modules to find one which includes me. */
567 if ((size_t) addr >= (size_t) me32.modBaseAddr &&
568 (size_t) addr < (size_t) (me32.modBaseAddr + me32.modBaseSize)) {
569 (*close_snap) (hModuleSnap);
572 # if _WIN32_WCE >= 101
573 return WideCharToMultiByte(CP_ACP, 0, me32.szExePath, -1,
574 path, sz, NULL, NULL);
577 int i, len = (int)wcslen(me32.szExePath);
582 for (i = 0; i < len; i++)
583 path[i] = (char)me32.szExePath[i];
590 int len = (int)strlen(me32.szExePath);
595 memcpy(path, me32.szExePath, len);
601 } while ((*module_next) (hModuleSnap, &me32));
603 (*close_snap) (hModuleSnap);
608 static void *win32_globallookup(const char *name)
611 HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
613 CREATETOOLHELP32SNAPSHOT create_snap;
614 CLOSETOOLHELP32SNAPSHOT close_snap;
615 MODULE32 module_first, module_next;
621 dll = LoadLibrary(TEXT(DLLNAME));
623 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
627 create_snap = (CREATETOOLHELP32SNAPSHOT)
628 GetProcAddress(dll, "CreateToolhelp32Snapshot");
629 if (create_snap == NULL) {
631 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
634 /* We take the rest for granted... */
636 close_snap = (CLOSETOOLHELP32SNAPSHOT)
637 GetProcAddress(dll, "CloseToolhelp32Snapshot");
639 close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
641 module_first = (MODULE32) GetProcAddress(dll, "Module32First");
642 module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
644 hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
645 if (hModuleSnap == INVALID_HANDLE_VALUE) {
647 ERR_raise(ERR_LIB_DSO, DSO_R_UNSUPPORTED);
651 me32.dwSize = sizeof(me32);
653 if (!(*module_first) (hModuleSnap, &me32)) {
654 (*close_snap) (hModuleSnap);
660 if ((ret.f = GetProcAddress(me32.hModule, name))) {
661 (*close_snap) (hModuleSnap);
665 } while ((*module_next) (hModuleSnap, &me32));
667 (*close_snap) (hModuleSnap);
671 #endif /* DSO_WIN32 */