]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/dso/dso_win32.c
Run util/openssl-format-source -v -c .
[thirdparty/openssl.git] / crypto / dso / dso_win32.c
CommitLineData
cbecb3ac 1/* dso_win32.c -*- mode:C; c-file-style: "eay" -*- */
40720ce3
MC
2/*
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4 * 2000.
8f4fac7f
GT
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
40720ce3 14 * notice, this list of conditions and the following disclaimer.
8f4fac7f
GT
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
60#include <stdio.h>
22a41547 61#include <string.h>
8f4fac7f
GT
62#include "cryptlib.h"
63#include <openssl/dso.h>
64
16420007 65#if !defined(DSO_WIN32)
8f4fac7f 66DSO_METHOD *DSO_METHOD_win32(void)
40720ce3
MC
67{
68 return NULL;
69}
8f4fac7f
GT
70#else
71
40720ce3
MC
72# ifdef _WIN32_WCE
73# if _WIN32_WCE < 300
74static FARPROC GetProcAddressA(HMODULE hModule, LPCSTR lpProcName)
75{
76 WCHAR lpProcNameW[64];
77 int i;
78
79 for (i = 0; lpProcName[i] && i < 64; i++)
80 lpProcNameW[i] = (WCHAR)lpProcName[i];
81 if (i == 64)
82 return NULL;
83 lpProcNameW[i] = 0;
84
85 return GetProcAddressW(hModule, lpProcNameW);
86}
87# endif
88# undef GetProcAddress
89# define GetProcAddress GetProcAddressA
67865069 90
2d54cc69 91static HINSTANCE LoadLibraryA(LPCSTR lpLibFileName)
40720ce3
MC
92{
93 WCHAR *fnamw;
94 size_t len_0 = strlen(lpLibFileName) + 1, i;
95
96# ifdef _MSC_VER
97 fnamw = (WCHAR *)_alloca(len_0 * sizeof(WCHAR));
98# else
99 fnamw = (WCHAR *)alloca(len_0 * sizeof(WCHAR));
100# endif
101 if (fnamw == NULL)
102 return NULL;
103
104# if defined(_WIN32_WCE) && _WIN32_WCE>=101
105 if (!MultiByteToWideChar(CP_ACP, 0, lpLibFileName, len_0, fnamw, len_0))
106# endif
107 for (i = 0; i < len_0; i++)
108 fnamw[i] = (WCHAR)lpLibFileName[i];
109
110 return LoadLibraryW(fnamw);
111}
112# endif
beae6324 113
b9e63915 114/* Part of the hack in "win32_load" ... */
40720ce3 115# define DSO_MAX_TRANSLATED_SIZE 256
b9e63915 116
51c8dc37 117static int win32_load(DSO *dso);
8f4fac7f 118static int win32_unload(DSO *dso);
e9a68cfb
GT
119static void *win32_bind_var(DSO *dso, const char *symname);
120static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
40720ce3 121# if 0
e9a68cfb
GT
122static int win32_unbind_var(DSO *dso, char *symname, void *symptr);
123static int win32_unbind_func(DSO *dso, char *symname, DSO_FUNC_TYPE symptr);
8f4fac7f
GT
124static int win32_init(DSO *dso);
125static int win32_finish(DSO *dso);
b9e63915 126static long win32_ctrl(DSO *dso, int cmd, long larg, void *parg);
40720ce3 127# endif
51c8dc37 128static char *win32_name_converter(DSO *dso, const char *filename);
cbecb3ac 129static char *win32_merger(DSO *dso, const char *filespec1,
40720ce3 130 const char *filespec2);
8f4fac7f 131
74e3931f
DSH
132static const char *openssl_strnchr(const char *string, int c, size_t len);
133
8f4fac7f 134static DSO_METHOD dso_meth_win32 = {
40720ce3
MC
135 "OpenSSL 'win32' shared library method",
136 win32_load,
137 win32_unload,
138 win32_bind_var,
139 win32_bind_func,
8f4fac7f 140/* For now, "unbind" doesn't exist */
40720ce3
MC
141# if 0
142 NULL, /* unbind_var */
143 NULL, /* unbind_func */
144# endif
145 NULL, /* ctrl */
146 win32_name_converter,
147 win32_merger,
148 NULL, /* init */
149 NULL /* finish */
150};
8f4fac7f
GT
151
152DSO_METHOD *DSO_METHOD_win32(void)
40720ce3
MC
153{
154 return (&dso_meth_win32);
155}
8f4fac7f 156
40720ce3
MC
157/*
158 * For this DSO_METHOD, our meth_data STACK will contain; (i) a pointer to
159 * the handle (HINSTANCE) returned from LoadLibrary(), and copied.
8f4fac7f
GT
160 */
161
51c8dc37 162static int win32_load(DSO *dso)
40720ce3
MC
163{
164 HINSTANCE h = NULL, *p = NULL;
165 /* See applicable comments from dso_dl.c */
166 char *filename = DSO_convert_filename(dso, NULL);
167
168 if (filename == NULL) {
169 DSOerr(DSO_F_WIN32_LOAD, DSO_R_NO_FILENAME);
170 goto err;
171 }
172 h = LoadLibraryA(filename);
173 if (h == NULL) {
174 DSOerr(DSO_F_WIN32_LOAD, DSO_R_LOAD_FAILED);
175 ERR_add_error_data(3, "filename(", filename, ")");
176 goto err;
177 }
178 p = (HINSTANCE *) OPENSSL_malloc(sizeof(HINSTANCE));
179 if (p == NULL) {
180 DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
181 goto err;
182 }
183 *p = h;
184 if (!sk_push(dso->meth_data, (char *)p)) {
185 DSOerr(DSO_F_WIN32_LOAD, DSO_R_STACK_ERROR);
186 goto err;
187 }
188 /* Success */
189 dso->loaded_filename = filename;
190 return (1);
191 err:
192 /* Cleanup ! */
193 if (filename != NULL)
194 OPENSSL_free(filename);
195 if (p != NULL)
196 OPENSSL_free(p);
197 if (h != NULL)
198 FreeLibrary(h);
199 return (0);
200}
8f4fac7f
GT
201
202static int win32_unload(DSO *dso)
40720ce3
MC
203{
204 HINSTANCE *p;
205 if (dso == NULL) {
206 DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
207 return (0);
208 }
209 if (sk_num(dso->meth_data) < 1)
210 return (1);
211 p = (HINSTANCE *) sk_pop(dso->meth_data);
212 if (p == NULL) {
213 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
214 return (0);
215 }
216 if (!FreeLibrary(*p)) {
217 DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
218 /*
219 * We should push the value back onto the stack in case of a retry.
220 */
221 sk_push(dso->meth_data, (char *)p);
222 return (0);
223 }
224 /* Cleanup */
225 OPENSSL_free(p);
226 return (1);
227}
228
229/*
230 * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
231 * docs, there's probably a variant for variables.
232 */
e9a68cfb 233static void *win32_bind_var(DSO *dso, const char *symname)
40720ce3
MC
234{
235 HINSTANCE *ptr;
236 void *sym;
237
238 if ((dso == NULL) || (symname == NULL)) {
239 DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
240 return (NULL);
241 }
242 if (sk_num(dso->meth_data) < 1) {
243 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
244 return (NULL);
245 }
246 ptr = (HINSTANCE *) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
247 if (ptr == NULL) {
248 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
249 return (NULL);
250 }
251 sym = GetProcAddress(*ptr, symname);
252 if (sym == NULL) {
253 DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
254 ERR_add_error_data(3, "symname(", symname, ")");
255 return (NULL);
256 }
257 return (sym);
258}
e9a68cfb
GT
259
260static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
40720ce3
MC
261{
262 HINSTANCE *ptr;
263 void *sym;
264
265 if ((dso == NULL) || (symname == NULL)) {
266 DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
267 return (NULL);
268 }
269 if (sk_num(dso->meth_data) < 1) {
270 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
271 return (NULL);
272 }
273 ptr = (HINSTANCE *) sk_value(dso->meth_data, sk_num(dso->meth_data) - 1);
274 if (ptr == NULL) {
275 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
276 return (NULL);
277 }
278 sym = GetProcAddress(*ptr, symname);
279 if (sym == NULL) {
280 DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
281 ERR_add_error_data(3, "symname(", symname, ")");
282 return (NULL);
283 }
284 return ((DSO_FUNC_TYPE)sym);
285}
286
287struct file_st {
288 const char *node;
289 int nodelen;
290 const char *device;
291 int devicelen;
292 const char *predir;
293 int predirlen;
294 const char *dir;
295 int dirlen;
296 const char *file;
297 int filelen;
298};
cbecb3ac
RL
299
300static struct file_st *win32_splitter(DSO *dso, const char *filename,
40720ce3
MC
301 int assume_last_is_dir)
302{
303 struct file_st *result = NULL;
304 enum { IN_NODE, IN_DEVICE, IN_FILE } position;
305 const char *start = filename;
306 char last;
307
308 if (!filename) {
309 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
310 /*
311 * goto err;
312 */
313 return (NULL);
314 }
315
316 result = OPENSSL_malloc(sizeof(struct file_st));
317 if (result == NULL) {
318 DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
319 return (NULL);
320 }
321
322 memset(result, 0, sizeof(struct file_st));
323 position = IN_DEVICE;
324
325 if ((filename[0] == '\\' && filename[1] == '\\')
326 || (filename[0] == '/' && filename[1] == '/')) {
327 position = IN_NODE;
328 filename += 2;
329 start = filename;
330 result->node = start;
331 }
332
333 do {
334 last = filename[0];
335 switch (last) {
336 case ':':
337 if (position != IN_DEVICE) {
338 DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
339 /*
340 * goto err;
341 */
342 OPENSSL_free(result);
343 return (NULL);
344 }
345 result->device = start;
346 result->devicelen = filename - start;
347 position = IN_FILE;
348 start = ++filename;
349 result->dir = start;
350 break;
351 case '\\':
352 case '/':
353 if (position == IN_NODE) {
354 result->nodelen = filename - start;
355 position = IN_FILE;
356 start = ++filename;
357 result->dir = start;
358 } else if (position == IN_DEVICE) {
359 position = IN_FILE;
360 filename++;
361 result->dir = start;
362 result->dirlen = filename - start;
363 start = filename;
364 } else {
365 filename++;
366 result->dirlen += filename - start;
367 start = filename;
368 }
369 break;
370 case '\0':
371 if (position == IN_NODE) {
372 result->nodelen = filename - start;
373 } else {
374 if (filename - start > 0) {
375 if (assume_last_is_dir) {
376 if (position == IN_DEVICE) {
377 result->dir = start;
378 result->dirlen = 0;
379 }
380 result->dirlen += filename - start;
381 } else {
382 result->file = start;
383 result->filelen = filename - start;
384 }
385 }
386 }
387 break;
388 default:
389 filename++;
390 break;
391 }
392 }
393 while (last);
394
395 if (!result->nodelen)
396 result->node = NULL;
397 if (!result->devicelen)
398 result->device = NULL;
399 if (!result->dirlen)
400 result->dir = NULL;
401 if (!result->filelen)
402 result->file = NULL;
403
404 return (result);
405}
cbecb3ac 406
74e3931f 407static char *win32_joiner(DSO *dso, const struct file_st *file_split)
40720ce3
MC
408{
409 int len = 0, offset = 0;
410 char *result = NULL;
411 const char *start;
412
413 if (!file_split) {
414 DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
415 return (NULL);
416 }
417 if (file_split->node) {
418 len += 2 + file_split->nodelen; /* 2 for starting \\ */
419 if (file_split->predir || file_split->dir || file_split->file)
420 len++; /* 1 for ending \ */
421 } else if (file_split->device) {
422 len += file_split->devicelen + 1; /* 1 for ending : */
423 }
424 len += file_split->predirlen;
425 if (file_split->predir && (file_split->dir || file_split->file)) {
426 len++; /* 1 for ending \ */
427 }
428 len += file_split->dirlen;
429 if (file_split->dir && file_split->file) {
430 len++; /* 1 for ending \ */
431 }
432 len += file_split->filelen;
433
434 if (!len) {
435 DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
436 return (NULL);
437 }
438
439 result = OPENSSL_malloc(len + 1);
440 if (!result) {
441 DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
442 return (NULL);
443 }
444
445 if (file_split->node) {
446 strcpy(&result[offset], "\\\\");
447 offset += 2;
448 strncpy(&result[offset], file_split->node, file_split->nodelen);
449 offset += file_split->nodelen;
450 if (file_split->predir || file_split->dir || file_split->file) {
451 result[offset] = '\\';
452 offset++;
453 }
454 } else if (file_split->device) {
455 strncpy(&result[offset], file_split->device, file_split->devicelen);
456 offset += file_split->devicelen;
457 result[offset] = ':';
458 offset++;
459 }
460 start = file_split->predir;
461 while (file_split->predirlen > (start - file_split->predir)) {
462 const char *end = openssl_strnchr(start, '/',
463 file_split->predirlen - (start -
464 file_split->predir));
465 if (!end)
466 end = start
467 + file_split->predirlen - (start - file_split->predir);
468 strncpy(&result[offset], start, end - start);
469 offset += end - start;
470 result[offset] = '\\';
471 offset++;
472 start = end + 1;
473 }
474# if 0 /* Not needed, since the directory converter
475 * above already appeneded a backslash */
476 if (file_split->predir && (file_split->dir || file_split->file)) {
477 result[offset] = '\\';
478 offset++;
479 }
480# endif
481 start = file_split->dir;
482 while (file_split->dirlen > (start - file_split->dir)) {
483 const char *end = openssl_strnchr(start, '/',
484 file_split->dirlen - (start -
485 file_split->dir));
486 if (!end)
487 end = start + file_split->dirlen - (start - file_split->dir);
488 strncpy(&result[offset], start, end - start);
489 offset += end - start;
490 result[offset] = '\\';
491 offset++;
492 start = end + 1;
493 }
494# if 0 /* Not needed, since the directory converter
495 * above already appeneded a backslash */
496 if (file_split->dir && file_split->file) {
497 result[offset] = '\\';
498 offset++;
499 }
500# endif
501 strncpy(&result[offset], file_split->file, file_split->filelen);
502 offset += file_split->filelen;
503 result[offset] = '\0';
504 return (result);
505}
506
507static char *win32_merger(DSO *dso, const char *filespec1,
508 const char *filespec2)
509{
510 char *merged = NULL;
511 struct file_st *filespec1_split = NULL;
512 struct file_st *filespec2_split = NULL;
513
514 if (!filespec1 && !filespec2) {
515 DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
516 return (NULL);
517 }
518 if (!filespec2) {
519 merged = OPENSSL_malloc(strlen(filespec1) + 1);
520 if (!merged) {
521 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
522 return (NULL);
523 }
524 strcpy(merged, filespec1);
525 } else if (!filespec1) {
526 merged = OPENSSL_malloc(strlen(filespec2) + 1);
527 if (!merged) {
528 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
529 return (NULL);
530 }
531 strcpy(merged, filespec2);
532 } else {
533 filespec1_split = win32_splitter(dso, filespec1, 0);
534 if (!filespec1_split) {
535 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
536 return (NULL);
537 }
538 filespec2_split = win32_splitter(dso, filespec2, 1);
539 if (!filespec2_split) {
540 DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
541 OPENSSL_free(filespec1_split);
542 return (NULL);
543 }
544
545 /* Fill in into filespec1_split */
546 if (!filespec1_split->node && !filespec1_split->device) {
547 filespec1_split->node = filespec2_split->node;
548 filespec1_split->nodelen = filespec2_split->nodelen;
549 filespec1_split->device = filespec2_split->device;
550 filespec1_split->devicelen = filespec2_split->devicelen;
551 }
552 if (!filespec1_split->dir) {
553 filespec1_split->dir = filespec2_split->dir;
554 filespec1_split->dirlen = filespec2_split->dirlen;
555 } else if (filespec1_split->dir[0] != '\\'
556 && filespec1_split->dir[0] != '/') {
557 filespec1_split->predir = filespec2_split->dir;
558 filespec1_split->predirlen = filespec2_split->dirlen;
559 }
560 if (!filespec1_split->file) {
561 filespec1_split->file = filespec2_split->file;
562 filespec1_split->filelen = filespec2_split->filelen;
563 }
564
565 merged = win32_joiner(dso, filespec1_split);
566 }
567 OPENSSL_free(filespec1_split);
568 OPENSSL_free(filespec2_split);
569 return (merged);
570}
cbecb3ac 571
51c8dc37 572static char *win32_name_converter(DSO *dso, const char *filename)
40720ce3
MC
573{
574 char *translated;
575 int len, transform;
576
577 len = strlen(filename);
578 transform = ((strstr(filename, "/") == NULL) &&
579 (strstr(filename, "\\") == NULL) &&
580 (strstr(filename, ":") == NULL));
581 if (transform)
582 /* We will convert this to "%s.dll" */
583 translated = OPENSSL_malloc(len + 5);
584 else
585 /* We will simply duplicate filename */
586 translated = OPENSSL_malloc(len + 1);
587 if (translated == NULL) {
588 DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
589 return (NULL);
590 }
591 if (transform)
592 sprintf(translated, "%s.dll", filename);
593 else
594 sprintf(translated, "%s", filename);
595 return (translated);
596}
51c8dc37 597
74e3931f 598static const char *openssl_strnchr(const char *string, int c, size_t len)
40720ce3
MC
599{
600 size_t i;
601 const char *p;
602 for (i = 0, p = string; i < len && *p; i++, p++) {
603 if (*p == c)
604 return p;
605 }
606 return NULL;
607}
608
609#endif /* OPENSSL_SYS_WIN32 */