dso_win32.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. #include "e_os.h"
  10. #include "dso_locl.h"
  11. #if defined(DSO_WIN32)
  12. # ifdef _WIN32_WCE
  13. # if _WIN32_WCE < 300
  14. static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
  15. {
  16. WCHAR lpProcNameW[64];
  17. int i;
  18. for (i = 0; lpProcName[i] && i < 64; i++)
  19. lpProcNameW[i] = (WCHAR)lpProcName[i];
  20. if (i == 64)
  21. return NULL;
  22. lpProcNameW[i] = 0;
  23. return GetProcAddressW(hModule, lpProcNameW);
  24. }
  25. # endif
  26. # undef GetProcAddress
  27. # define GetProcAddress GetProcAddressA
  28. static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
  29. {
  30. WCHAR *fnamw;
  31. size_t len_0 = strlen(lpLibFileName) + 1, i;
  32. # ifdef _MSC_VER
  33. fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
  34. # else
  35. fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
  36. # endif
  37. if (fnamw == NULL) {
  38. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  39. return NULL;
  40. }
  41. # if defined(_WIN32_WCE) && _WIN32_WCE>=101
  42. if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
  43. # endif
  44. for (i = 0; i < len_0; i++)
  45. fnamw[i] = (WCHAR)lpLibFileName[i];
  46. return LoadLibraryW(fnamw);
  47. }
  48. # endif
  49. /* Part of the hack in "win32_load" ... */
  50. # define DSO_MAX_TRANSLATED_SIZE 256
  51. static int win32_load(DSO *dso);
  52. static int win32_unload(DSO *dso);
  53. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
  54. static char *win32_name_converter(DSO *dso, const char *filename);
  55. static char *win32_merger(DSO *dso, const char *filespec1,
  56. const char *filespec2);
  57. static void *win32_globallookup(const char *name);
  58. static const char *openssl_strnchr(const char *string, int c, size_t len);
  59. static DSO_METHOD dso_meth_win32 = {
  60. "OpenSSL 'win32' shared library method",
  61. win32_load,
  62. win32_unload,
  63. win32_bind_func,
  64. NULL, /* ctrl */
  65. win32_name_converter,
  66. win32_merger,
  67. NULL, /* init */
  68. NULL, /* finish */
  69. NULL, /* pathbyaddr */
  70. win32_globallookup
  71. };
  72. DSO_METHOD *DSO_METHOD_openssl(void)
  73. {
  74. return &dso_meth_win32;
  75. }
  76. /*
  77. * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
  78. * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
  79. */
  80. static int win32_load(DSO *dso)
  81. {
  82. HINSTANCE h = NULL, *p = NULL;
  83. /* See applicable comments from dso_dl.c */
  84. char *filename = DSO_convert_filename(dso, NULL);
  85. if (filename == NULL) {
  86. DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
  87. goto err;
  88. }
  89. h = LoadLibraryA(filename);
  90. if (h == NULL) {
  91. DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
  92. ERR_add_error_data(3, "filename(", filename, ")");
  93. goto err;
  94. }
  95. p = OPENSSL_malloc(sizeof(*p));
  96. if (p == NULL) {
  97. DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
  98. goto err;
  99. }
  100. *p = h;
  101. if (!sk_void_push(dso->meth_data, p)) {
  102. DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
  103. goto err;
  104. }
  105. /* Success */
  106. dso->loaded_filename = filename;
  107. return 1;
  108. err:
  109. /* Cleanup ! */
  110. OPENSSL_free(filename);
  111. OPENSSL_free(p);
  112. if (h != NULL)
  113. FreeLibrary(h);
  114. return 0;
  115. }
  116. static int win32_unload(DSO *dso)
  117. {
  118. HINSTANCE *p;
  119. if (dso == NULL) {
  120. DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  121. return 0;
  122. }
  123. if (sk_void_num(dso->meth_data) < 1)
  124. return 1;
  125. p = sk_void_pop(dso->meth_data);
  126. if (p == NULL) {
  127. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
  128. return 0;
  129. }
  130. if (!FreeLibrary(*p)) {
  131. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
  132. /*
  133. * We should push the value back onto the stack in case of a retry.
  134. */
  135. sk_void_push(dso->meth_data, p);
  136. return 0;
  137. }
  138. /* Cleanup */
  139. OPENSSL_free(p);
  140. return 1;
  141. }
  142. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
  143. {
  144. HINSTANCE *ptr;
  145. union {
  146. void *p;
  147. FARPROC f;
  148. } sym;
  149. if ((dso == NULL) || (symname == NULL)) {
  150. DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  151. return NULL;
  152. }
  153. if (sk_void_num(dso->meth_data) < 1) {
  154. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
  155. return NULL;
  156. }
  157. ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
  158. if (ptr == NULL) {
  159. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
  160. return NULL;
  161. }
  162. sym.f = GetProcAddress(*ptr, symname);
  163. if (sym.p == NULL) {
  164. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
  165. ERR_add_error_data(3, "symname(", symname, ")");
  166. return NULL;
  167. }
  168. return (DSO_FUNC_TYPE)sym.f;
  169. }
  170. struct file_st {
  171. const char *node;
  172. int nodelen;
  173. const char *device;
  174. int devicelen;
  175. const char *predir;
  176. int predirlen;
  177. const char *dir;
  178. int dirlen;
  179. const char *file;
  180. int filelen;
  181. };
  182. static struct file_st *win32_splitter(DSO *dso, const char *filename,
  183. int assume_last_is_dir)
  184. {
  185. struct file_st *result = NULL;
  186. enum { IN_NODE, IN_DEVICE, IN_FILE } position;
  187. const char *start = filename;
  188. char last;
  189. if (!filename) {
  190. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
  191. return NULL;
  192. }
  193. result = OPENSSL_zalloc(sizeof(*result));
  194. if (result == NULL) {
  195. DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
  196. return NULL;
  197. }
  198. position = IN_DEVICE;
  199. if ((filename[0] == '\\' && filename[1] == '\\')
  200. || (filename[0] == '/' && filename[1] == '/')) {
  201. position = IN_NODE;
  202. filename += 2;
  203. start = filename;
  204. result->node = start;
  205. }
  206. do {
  207. last = filename[0];
  208. switch (last) {
  209. case ':':
  210. if (position != IN_DEVICE) {
  211. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
  212. OPENSSL_free(result);
  213. return NULL;
  214. }
  215. result->device = start;
  216. result->devicelen = (int)(filename - start);
  217. position = IN_FILE;
  218. start = ++filename;
  219. result->dir = start;
  220. break;
  221. case '\\':
  222. case '/':
  223. if (position == IN_NODE) {
  224. result->nodelen = (int)(filename - start);
  225. position = IN_FILE;
  226. start = ++filename;
  227. result->dir = start;
  228. } else if (position == IN_DEVICE) {
  229. position = IN_FILE;
  230. filename++;
  231. result->dir = start;
  232. result->dirlen = (int)(filename - start);
  233. start = filename;
  234. } else {
  235. filename++;
  236. result->dirlen += (int)(filename - start);
  237. start = filename;
  238. }
  239. break;
  240. case '\0':
  241. if (position == IN_NODE) {
  242. result->nodelen = (int)(filename - start);
  243. } else {
  244. if (filename - start > 0) {
  245. if (assume_last_is_dir) {
  246. if (position == IN_DEVICE) {
  247. result->dir = start;
  248. result->dirlen = 0;
  249. }
  250. result->dirlen += (int)(filename - start);
  251. } else {
  252. result->file = start;
  253. result->filelen = (int)(filename - start);
  254. }
  255. }
  256. }
  257. break;
  258. default:
  259. filename++;
  260. break;
  261. }
  262. }
  263. while (last);
  264. if (!result->nodelen)
  265. result->node = NULL;
  266. if (!result->devicelen)
  267. result->device = NULL;
  268. if (!result->dirlen)
  269. result->dir = NULL;
  270. if (!result->filelen)
  271. result->file = NULL;
  272. return result;
  273. }
  274. static char *win32_joiner(DSO *dso, const struct file_st *file_split)
  275. {
  276. int len = 0, offset = 0;
  277. char *result = NULL;
  278. const char *start;
  279. if (!file_split) {
  280. DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
  281. return NULL;
  282. }
  283. if (file_split->node) {
  284. len += 2 + file_split->nodelen; /* 2 for starting \\ */
  285. if (file_split->predir || file_split->dir || file_split->file)
  286. len++; /* 1 for ending \ */
  287. } else if (file_split->device) {
  288. len += file_split->devicelen + 1; /* 1 for ending : */
  289. }
  290. len += file_split->predirlen;
  291. if (file_split->predir && (file_split->dir || file_split->file)) {
  292. len++; /* 1 for ending \ */
  293. }
  294. len += file_split->dirlen;
  295. if (file_split->dir && file_split->file) {
  296. len++; /* 1 for ending \ */
  297. }
  298. len += file_split->filelen;
  299. if (!len) {
  300. DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
  301. return NULL;
  302. }
  303. result = OPENSSL_malloc(len + 1);
  304. if (result == NULL) {
  305. DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
  306. return NULL;
  307. }
  308. if (file_split->node) {
  309. strcpy(&result[offset], "\\\\");
  310. offset += 2;
  311. strncpy(&result[offset], file_split->node, file_split->nodelen);
  312. offset += file_split->nodelen;
  313. if (file_split->predir || file_split->dir || file_split->file) {
  314. result[offset] = '\\';
  315. offset++;
  316. }
  317. } else if (file_split->device) {
  318. strncpy(&result[offset], file_split->device, file_split->devicelen);
  319. offset += file_split->devicelen;
  320. result[offset] = ':';
  321. offset++;
  322. }
  323. start = file_split->predir;
  324. while (file_split->predirlen > (start - file_split->predir)) {
  325. const char *end = openssl_strnchr(start, '/',
  326. file_split->predirlen - (start -
  327. file_split->predir));
  328. if (!end)
  329. end = start
  330. + file_split->predirlen - (start - file_split->predir);
  331. strncpy(&result[offset], start, end - start);
  332. offset += (int)(end - start);
  333. result[offset] = '\\';
  334. offset++;
  335. start = end + 1;
  336. }
  337. start = file_split->dir;
  338. while (file_split->dirlen > (start - file_split->dir)) {
  339. const char *end = openssl_strnchr(start, '/',
  340. file_split->dirlen - (start -
  341. file_split->dir));
  342. if (!end)
  343. end = start + file_split->dirlen - (start - file_split->dir);
  344. strncpy(&result[offset], start, end - start);
  345. offset += (int)(end - start);
  346. result[offset] = '\\';
  347. offset++;
  348. start = end + 1;
  349. }
  350. strncpy(&result[offset], file_split->file, file_split->filelen);
  351. offset += file_split->filelen;
  352. result[offset] = '\0';
  353. return result;
  354. }
  355. static char *win32_merger(DSO *dso, const char *filespec1,
  356. const char *filespec2)
  357. {
  358. char *merged = NULL;
  359. struct file_st *filespec1_split = NULL;
  360. struct file_st *filespec2_split = NULL;
  361. if (!filespec1 && !filespec2) {
  362. DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  363. return NULL;
  364. }
  365. if (!filespec2) {
  366. merged = OPENSSL_strdup(filespec1);
  367. if (merged == NULL) {
  368. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  369. return NULL;
  370. }
  371. } else if (!filespec1) {
  372. merged = OPENSSL_strdup(filespec2);
  373. if (merged == NULL) {
  374. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  375. return NULL;
  376. }
  377. } else {
  378. filespec1_split = win32_splitter(dso, filespec1, 0);
  379. if (!filespec1_split) {
  380. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  381. return NULL;
  382. }
  383. filespec2_split = win32_splitter(dso, filespec2, 1);
  384. if (!filespec2_split) {
  385. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  386. OPENSSL_free(filespec1_split);
  387. return NULL;
  388. }
  389. /* Fill in into filespec1_split */
  390. if (!filespec1_split->node && !filespec1_split->device) {
  391. filespec1_split->node = filespec2_split->node;
  392. filespec1_split->nodelen = filespec2_split->nodelen;
  393. filespec1_split->device = filespec2_split->device;
  394. filespec1_split->devicelen = filespec2_split->devicelen;
  395. }
  396. if (!filespec1_split->dir) {
  397. filespec1_split->dir = filespec2_split->dir;
  398. filespec1_split->dirlen = filespec2_split->dirlen;
  399. } else if (filespec1_split->dir[0] != '\\'
  400. && filespec1_split->dir[0] != '/') {
  401. filespec1_split->predir = filespec2_split->dir;
  402. filespec1_split->predirlen = filespec2_split->dirlen;
  403. }
  404. if (!filespec1_split->file) {
  405. filespec1_split->file = filespec2_split->file;
  406. filespec1_split->filelen = filespec2_split->filelen;
  407. }
  408. merged = win32_joiner(dso, filespec1_split);
  409. }
  410. OPENSSL_free(filespec1_split);
  411. OPENSSL_free(filespec2_split);
  412. return merged;
  413. }
  414. static char *win32_name_converter(DSO *dso, const char *filename)
  415. {
  416. char *translated;
  417. int len, transform;
  418. len = strlen(filename);
  419. transform = ((strstr(filename, "/") == NULL) &&
  420. (strstr(filename, "\\") == NULL) &&
  421. (strstr(filename, ":") == NULL));
  422. if (transform)
  423. /* We will convert this to "%s.dll" */
  424. translated = OPENSSL_malloc(len + 5);
  425. else
  426. /* We will simply duplicate filename */
  427. translated = OPENSSL_malloc(len + 1);
  428. if (translated == NULL) {
  429. DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  430. return NULL;
  431. }
  432. if (transform)
  433. sprintf(translated, "%s.dll", filename);
  434. else
  435. sprintf(translated, "%s", filename);
  436. return translated;
  437. }
  438. static const char *openssl_strnchr(const char *string, int c, size_t len)
  439. {
  440. size_t i;
  441. const char *p;
  442. for (i = 0, p = string; i < len && *p; i++, p++) {
  443. if (*p == c)
  444. return p;
  445. }
  446. return NULL;
  447. }
  448. # include <tlhelp32.h>
  449. # ifdef _WIN32_WCE
  450. # define DLLNAME "TOOLHELP.DLL"
  451. # else
  452. # ifdef MODULEENTRY32
  453. # undef MODULEENTRY32 /* unmask the ASCII version! */
  454. # endif
  455. # define DLLNAME "KERNEL32.DLL"
  456. # endif
  457. typedef HANDLE(WINAPI *CREATETOOLHELP32SNAPSHOT) (DWORD, DWORD);
  458. typedef BOOL(WINAPI *CLOSETOOLHELP32SNAPSHOT) (HANDLE);
  459. typedef BOOL(WINAPI *MODULE32) (HANDLE, MODULEENTRY32 *);
  460. static void *win32_globallookup(const char *name)
  461. {
  462. HMODULE dll;
  463. HANDLE hModuleSnap = INVALID_HANDLE_VALUE;
  464. MODULEENTRY32 me32;
  465. CREATETOOLHELP32SNAPSHOT create_snap;
  466. CLOSETOOLHELP32SNAPSHOT close_snap;
  467. MODULE32 module_first, module_next;
  468. union {
  469. void *p;
  470. FARPROC f;
  471. } ret = { NULL };
  472. dll = LoadLibrary(TEXT(DLLNAME));
  473. if (dll == NULL) {
  474. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  475. return NULL;
  476. }
  477. create_snap = (CREATETOOLHELP32SNAPSHOT)
  478. GetProcAddress(dll, "CreateToolhelp32Snapshot");
  479. if (create_snap == NULL) {
  480. FreeLibrary(dll);
  481. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  482. return NULL;
  483. }
  484. /* We take the rest for granted... */
  485. # ifdef _WIN32_WCE
  486. close_snap = (CLOSETOOLHELP32SNAPSHOT)
  487. GetProcAddress(dll, "CloseToolhelp32Snapshot");
  488. # else
  489. close_snap = (CLOSETOOLHELP32SNAPSHOT) CloseHandle;
  490. # endif
  491. module_first = (MODULE32) GetProcAddress(dll, "Module32First");
  492. module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
  493. hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
  494. if (hModuleSnap == INVALID_HANDLE_VALUE) {
  495. FreeLibrary(dll);
  496. DSOerr(DSO_F_WIN32_GLOBALLOOKUP, DSO_R_UNSUPPORTED);
  497. return NULL;
  498. }
  499. me32.dwSize = sizeof(me32);
  500. if (!(*module_first) (hModuleSnap, &me32)) {
  501. (*close_snap) (hModuleSnap);
  502. FreeLibrary(dll);
  503. return NULL;
  504. }
  505. do {
  506. if ((ret.f = GetProcAddress(me32.hModule, name))) {
  507. (*close_snap) (hModuleSnap);
  508. FreeLibrary(dll);
  509. return ret.p;
  510. }
  511. } while ((*module_next) (hModuleSnap, &me32));
  512. (*close_snap) (hModuleSnap);
  513. FreeLibrary(dll);
  514. return NULL;
  515. }
  516. #endif /* DSO_WIN32 */