dso_win32.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* dso_win32.c -*- mode:C; c-file-style: "eay" -*- */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include "cryptlib.h"
  62. #include <openssl/dso.h>
  63. #if !defined(DSO_WIN32)
  64. DSO_METHOD *DSO_METHOD_win32(void)
  65. {
  66. return NULL;
  67. }
  68. #else
  69. # ifdef _WIN32_WCE
  70. # if _WIN32_WCE < 300
  71. static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
  72. {
  73. WCHAR lpProcNameW[64];
  74. int i;
  75. for (i = 0; lpProcName[i] && i < 64; i++)
  76. lpProcNameW[i] = (WCHAR)lpProcName[i];
  77. if (i == 64)
  78. return NULL;
  79. lpProcNameW[i] = 0;
  80. return GetProcAddressW(hModule, lpProcNameW);
  81. }
  82. # endif
  83. # undef GetProcAddress
  84. # define GetProcAddress GetProcAddressA
  85. static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
  86. {
  87. WCHAR *fnamw;
  88. size_t len_0 = strlen(lpLibFileName) + 1, i;
  89. # ifdef _MSC_VER
  90. fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
  91. # else
  92. fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
  93. # endif
  94. if (fnamw == NULL)
  95. return NULL;
  96. # if defined(_WIN32_WCE) && _WIN32_WCE>=101
  97. if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
  98. # endif
  99. for (i = 0; i < len_0; i++)
  100. fnamw[i] = (WCHAR)lpLibFileName[i];
  101. return LoadLibraryW(fnamw);
  102. }
  103. # endif
  104. /* Part of the hack in "win32_load" ... */
  105. # define DSO_MAX_TRANSLATED_SIZE 256
  106. static int win32_load(DSO *dso);
  107. static int win32_unload(DSO *dso);
  108. static void *win32_bind_var(DSO *dso, const char *symname);
  109. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
  110. # if 0
  111. static int win32_unbind_var(DSO *dso, char *symname, void *symptr);
  112. static int win32_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
  113. static int win32_init(DSO *dso);
  114. static int win32_finish(DSO *dso);
  115. static long win32_ctrl(DSO *dso, int cmd, long larg, void *parg);
  116. # endif
  117. static char *win32_name_converter(DSO *dso, const char *filename);
  118. static char *win32_merger(DSO *dso, const char *filespec1,
  119. const char *filespec2);
  120. 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",
  123. win32_load,
  124. win32_unload,
  125. win32_bind_var,
  126. win32_bind_func,
  127. /* For now, "unbind" doesn't exist */
  128. # if 0
  129. NULL, /* unbind_var */
  130. NULL, /* unbind_func */
  131. # endif
  132. NULL, /* ctrl */
  133. win32_name_converter,
  134. win32_merger,
  135. NULL, /* init */
  136. NULL /* finish */
  137. };
  138. DSO_METHOD *DSO_METHOD_win32(void)
  139. {
  140. return (&dso_meth_win32);
  141. }
  142. /*
  143. * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
  144. * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
  145. */
  146. static int win32_load(DSO *dso)
  147. {
  148. HINSTANCE h = NULL, *p = NULL;
  149. /* See applicable comments from dso_dl.c */
  150. char *filename = DSO_convert_filename(dso, NULL);
  151. if (filename == NULL) {
  152. DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
  153. goto err;
  154. }
  155. h = LoadLibraryA(filename);
  156. if (h == NULL) {
  157. DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
  158. ERR_add_error_data(3, "filename(", filename, ")");
  159. goto err;
  160. }
  161. p = (HINSTANCE *) OPENSSL_malloc(sizeof(HINSTANCE));
  162. if (p == NULL) {
  163. DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
  164. goto err;
  165. }
  166. *p = h;
  167. if (!sk_push(dso->meth_data, (char *)p)) {
  168. DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
  169. goto err;
  170. }
  171. /* Success */
  172. dso->loaded_filename = filename;
  173. return (1);
  174. err:
  175. /* Cleanup ! */
  176. if (filename != NULL)
  177. OPENSSL_free(filename);
  178. if (p != NULL)
  179. OPENSSL_free(p);
  180. if (h != NULL)
  181. FreeLibrary(h);
  182. return (0);
  183. }
  184. static int win32_unload(DSO *dso)
  185. {
  186. HINSTANCE *p;
  187. if (dso == NULL) {
  188. DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
  189. return (0);
  190. }
  191. if (sk_num(dso->meth_data) < 1)
  192. return (1);
  193. p = (HINSTANCE *) sk_pop(dso->meth_data);
  194. if (p == NULL) {
  195. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
  196. return (0);
  197. }
  198. if (!FreeLibrary(*p)) {
  199. DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
  200. /*
  201. * We should push the value back onto the stack in case of a retry.
  202. */
  203. sk_push(dso->meth_data, (char *)p);
  204. return (0);
  205. }
  206. /* Cleanup */
  207. OPENSSL_free(p);
  208. return (1);
  209. }
  210. /*
  211. * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
  212. * docs, there's probably a variant for variables.
  213. */
  214. static void *win32_bind_var(DSO *dso, const char *symname)
  215. {
  216. HINSTANCE *ptr;
  217. void *sym;
  218. if ((dso == NULL) || (symname == NULL)) {
  219. DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
  220. return (NULL);
  221. }
  222. if (sk_num(dso->meth_data) < 1) {
  223. DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
  224. return (NULL);
  225. }
  226. ptr = (HINSTANCE *) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  227. if (ptr == NULL) {
  228. DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
  229. return (NULL);
  230. }
  231. sym = GetProcAddress(*ptr, symname);
  232. if (sym == NULL) {
  233. DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
  234. ERR_add_error_data(3, "symname(", symname, ")");
  235. return (NULL);
  236. }
  237. return (sym);
  238. }
  239. static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
  240. {
  241. HINSTANCE *ptr;
  242. void *sym;
  243. if ((dso == NULL) || (symname == NULL)) {
  244. DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
  245. return (NULL);
  246. }
  247. if (sk_num(dso->meth_data) < 1) {
  248. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
  249. return (NULL);
  250. }
  251. ptr = (HINSTANCE *) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
  252. if (ptr == NULL) {
  253. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
  254. return (NULL);
  255. }
  256. sym = GetProcAddress(*ptr, symname);
  257. if (sym == NULL) {
  258. DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
  259. ERR_add_error_data(3, "symname(", symname, ")");
  260. return (NULL);
  261. }
  262. return ((DSO_FUNC_TYPE)sym);
  263. }
  264. struct file_st {
  265. const char *node;
  266. int nodelen;
  267. const char *device;
  268. int devicelen;
  269. const char *predir;
  270. int predirlen;
  271. const char *dir;
  272. int dirlen;
  273. const char *file;
  274. int filelen;
  275. };
  276. static struct file_st *win32_splitter(DSO *dso, const char *filename,
  277. int assume_last_is_dir)
  278. {
  279. struct file_st *result = NULL;
  280. enum { IN_NODE, IN_DEVICE, IN_FILE } position;
  281. const char *start = filename;
  282. char last;
  283. if (!filename) {
  284. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
  285. /*
  286. * goto err;
  287. */
  288. return (NULL);
  289. }
  290. result = OPENSSL_malloc(sizeof(struct file_st));
  291. if (result == NULL) {
  292. DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
  293. return (NULL);
  294. }
  295. memset(result, 0, sizeof(struct file_st));
  296. position = IN_DEVICE;
  297. if ((filename[0] == '\\' && filename[1] == '\\')
  298. || (filename[0] == '/' && filename[1] == '/')) {
  299. position = IN_NODE;
  300. filename += 2;
  301. start = filename;
  302. result->node = start;
  303. }
  304. do {
  305. last = filename[0];
  306. switch (last) {
  307. case ':':
  308. if (position != IN_DEVICE) {
  309. DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
  310. /*
  311. * goto err;
  312. */
  313. OPENSSL_free(result);
  314. return (NULL);
  315. }
  316. result->device = start;
  317. result->devicelen = filename - start;
  318. position = IN_FILE;
  319. start = ++filename;
  320. result->dir = start;
  321. break;
  322. case '\\':
  323. case '/':
  324. if (position == IN_NODE) {
  325. result->nodelen = filename - start;
  326. position = IN_FILE;
  327. start = ++filename;
  328. result->dir = start;
  329. } else if (position == IN_DEVICE) {
  330. position = IN_FILE;
  331. filename++;
  332. result->dir = start;
  333. result->dirlen = filename - start;
  334. start = filename;
  335. } else {
  336. filename++;
  337. result->dirlen += filename - start;
  338. start = filename;
  339. }
  340. break;
  341. case '\0':
  342. if (position == IN_NODE) {
  343. result->nodelen = filename - start;
  344. } else {
  345. if (filename - start > 0) {
  346. if (assume_last_is_dir) {
  347. if (position == IN_DEVICE) {
  348. result->dir = start;
  349. result->dirlen = 0;
  350. }
  351. result->dirlen += filename - start;
  352. } else {
  353. result->file = start;
  354. result->filelen = filename - start;
  355. }
  356. }
  357. }
  358. break;
  359. default:
  360. filename++;
  361. break;
  362. }
  363. }
  364. while (last);
  365. if (!result->nodelen)
  366. result->node = NULL;
  367. if (!result->devicelen)
  368. result->device = NULL;
  369. if (!result->dirlen)
  370. result->dir = NULL;
  371. if (!result->filelen)
  372. result->file = NULL;
  373. return (result);
  374. }
  375. static char *win32_joiner(DSO *dso, const struct file_st *file_split)
  376. {
  377. int len = 0, offset = 0;
  378. char *result = NULL;
  379. const char *start;
  380. if (!file_split) {
  381. DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
  382. return (NULL);
  383. }
  384. if (file_split->node) {
  385. len += 2 + file_split->nodelen; /* 2 for starting \\ */
  386. if (file_split->predir || file_split->dir || file_split->file)
  387. len++; /* 1 for ending \ */
  388. } else if (file_split->device) {
  389. len += file_split->devicelen + 1; /* 1 for ending : */
  390. }
  391. len += file_split->predirlen;
  392. if (file_split->predir && (file_split->dir || file_split->file)) {
  393. len++; /* 1 for ending \ */
  394. }
  395. len += file_split->dirlen;
  396. if (file_split->dir && file_split->file) {
  397. len++; /* 1 for ending \ */
  398. }
  399. len += file_split->filelen;
  400. if (!len) {
  401. DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
  402. return (NULL);
  403. }
  404. result = OPENSSL_malloc(len + 1);
  405. if (!result) {
  406. DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
  407. return (NULL);
  408. }
  409. if (file_split->node) {
  410. strcpy(&result[offset], "\\\\");
  411. offset += 2;
  412. strncpy(&result[offset], file_split->node, file_split->nodelen);
  413. offset += file_split->nodelen;
  414. if (file_split->predir || file_split->dir || file_split->file) {
  415. result[offset] = '\\';
  416. offset++;
  417. }
  418. } else if (file_split->device) {
  419. strncpy(&result[offset], file_split->device, file_split->devicelen);
  420. offset += file_split->devicelen;
  421. result[offset] = ':';
  422. offset++;
  423. }
  424. start = file_split->predir;
  425. while (file_split->predirlen > (start - file_split->predir)) {
  426. const char *end = openssl_strnchr(start, '/',
  427. file_split->predirlen - (start -
  428. file_split->predir));
  429. if (!end)
  430. end = start
  431. + file_split->predirlen - (start - file_split->predir);
  432. strncpy(&result[offset], start, end - start);
  433. offset += end - start;
  434. result[offset] = '\\';
  435. offset++;
  436. start = end + 1;
  437. }
  438. # if 0 /* Not needed, since the directory converter
  439. * above already appeneded a backslash */
  440. if (file_split->predir && (file_split->dir || file_split->file)) {
  441. result[offset] = '\\';
  442. offset++;
  443. }
  444. # endif
  445. start = file_split->dir;
  446. while (file_split->dirlen > (start - file_split->dir)) {
  447. const char *end = openssl_strnchr(start, '/',
  448. file_split->dirlen - (start -
  449. file_split->dir));
  450. if (!end)
  451. end = start + file_split->dirlen - (start - file_split->dir);
  452. strncpy(&result[offset], start, end - start);
  453. offset += end - start;
  454. result[offset] = '\\';
  455. offset++;
  456. start = end + 1;
  457. }
  458. # if 0 /* Not needed, since the directory converter
  459. * above already appeneded a backslash */
  460. if (file_split->dir && file_split->file) {
  461. result[offset] = '\\';
  462. offset++;
  463. }
  464. # endif
  465. strncpy(&result[offset], file_split->file, file_split->filelen);
  466. offset += file_split->filelen;
  467. result[offset] = '\0';
  468. return (result);
  469. }
  470. static char *win32_merger(DSO *dso, const char *filespec1,
  471. const char *filespec2)
  472. {
  473. char *merged = NULL;
  474. struct file_st *filespec1_split = NULL;
  475. struct file_st *filespec2_split = NULL;
  476. if (!filespec1 && !filespec2) {
  477. DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
  478. return (NULL);
  479. }
  480. if (!filespec2) {
  481. merged = OPENSSL_malloc(strlen(filespec1) + 1);
  482. if (!merged) {
  483. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  484. return (NULL);
  485. }
  486. strcpy(merged, filespec1);
  487. } else if (!filespec1) {
  488. merged = OPENSSL_malloc(strlen(filespec2) + 1);
  489. if (!merged) {
  490. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  491. return (NULL);
  492. }
  493. strcpy(merged, filespec2);
  494. } else {
  495. filespec1_split = win32_splitter(dso, filespec1, 0);
  496. if (!filespec1_split) {
  497. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  498. return (NULL);
  499. }
  500. filespec2_split = win32_splitter(dso, filespec2, 1);
  501. if (!filespec2_split) {
  502. DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
  503. OPENSSL_free(filespec1_split);
  504. return (NULL);
  505. }
  506. /* Fill in into filespec1_split */
  507. if (!filespec1_split->node && !filespec1_split->device) {
  508. filespec1_split->node = filespec2_split->node;
  509. filespec1_split->nodelen = filespec2_split->nodelen;
  510. filespec1_split->device = filespec2_split->device;
  511. filespec1_split->devicelen = filespec2_split->devicelen;
  512. }
  513. if (!filespec1_split->dir) {
  514. filespec1_split->dir = filespec2_split->dir;
  515. filespec1_split->dirlen = filespec2_split->dirlen;
  516. } else if (filespec1_split->dir[0] != '\\'
  517. && filespec1_split->dir[0] != '/') {
  518. filespec1_split->predir = filespec2_split->dir;
  519. filespec1_split->predirlen = filespec2_split->dirlen;
  520. }
  521. if (!filespec1_split->file) {
  522. filespec1_split->file = filespec2_split->file;
  523. filespec1_split->filelen = filespec2_split->filelen;
  524. }
  525. merged = win32_joiner(dso, filespec1_split);
  526. }
  527. OPENSSL_free(filespec1_split);
  528. OPENSSL_free(filespec2_split);
  529. return (merged);
  530. }
  531. static char *win32_name_converter(DSO *dso, const char *filename)
  532. {
  533. char *translated;
  534. int len, transform;
  535. len = strlen(filename);
  536. transform = ((strstr(filename, "/") == NULL) &&
  537. (strstr(filename, "\\") == NULL) &&
  538. (strstr(filename, ":") == NULL));
  539. if (transform)
  540. /* We will convert this to "%s.dll" */
  541. translated = OPENSSL_malloc(len + 5);
  542. else
  543. /* We will simply duplicate filename */
  544. translated = OPENSSL_malloc(len + 1);
  545. if (translated == NULL) {
  546. DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
  547. return (NULL);
  548. }
  549. if (transform)
  550. sprintf(translated, "%s.dll", filename);
  551. else
  552. sprintf(translated, "%s", filename);
  553. return (translated);
  554. }
  555. static const char *openssl_strnchr(const char *string, int c, size_t len)
  556. {
  557. size_t i;
  558. const char *p;
  559. for (i = 0, p = string; i < len && *p; i++, p++) {
  560. if (*p == c)
  561. return p;
  562. }
  563. return NULL;
  564. }
  565. #endif /* OPENSSL_SYS_WIN32 */