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