2
0

dso_win32.c 17 KB

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