-/* dso_win32.c -*- mode:C; c-file-style: "eay" -*- */
/*
- * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
- * 2000.
- */
-/* ====================================================================
- * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. All advertising materials mentioning features or use of this
- * software must display the following acknowledgment:
- * "This product includes software developed by the OpenSSL Project
- * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
- *
- * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
- * endorse or promote products derived from this software without
- * prior written permission. For written permission, please contact
- * licensing@OpenSSL.org.
- *
- * 5. Products derived from this software may not be called "OpenSSL"
- * nor may "OpenSSL" appear in their names without prior written
- * permission of the OpenSSL Project.
- *
- * 6. Redistributions of any form whatsoever must retain the following
- * acknowledgment:
- * "This product includes software developed by the OpenSSL Project
- * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
- *
- * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
- * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
- * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- * ====================================================================
- *
- * This product includes cryptographic software written by Eric Young
- * (eay@cryptsoft.com). This product includes software written by Tim
- * Hudson (tjh@cryptsoft.com).
+ * Copyright 2000-2017 The OpenSSL Project Authors. All Rights Reserved.
*
+ * Licensed under the Apache License 2.0 (the "License"). You may not use
+ * this file except in compliance with the License. You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
*/
-#include <stdio.h>
-#include <string.h>
-#include "cryptlib.h"
-#include <openssl/dso.h>
+#include "e_os.h"
+#include "dso_local.h"
-#if !defined(DSO_WIN32)
-DSO_METHOD *DSO_METHOD_win32(void)
-{
- return NULL;
-}
-#else
+#if defined(DSO_WIN32)
# ifdef _WIN32_WCE
# if _WIN32_WCE < 300
static int win32_load(DSO *dso);
static int win32_unload(DSO *dso);
-static void *win32_bind_var(DSO *dso, const char *symname);
static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname);
static char *win32_name_converter(DSO *dso, const char *filename);
static char *win32_merger(DSO *dso, const char *filespec1,
"OpenSSL 'win32' shared library method",
win32_load,
win32_unload,
- win32_bind_var,
win32_bind_func,
NULL, /* ctrl */
win32_name_converter,
win32_merger,
NULL, /* init */
NULL, /* finish */
- win32_pathbyaddr,
+ win32_pathbyaddr, /* pathbyaddr */
win32_globallookup
};
-DSO_METHOD *DSO_METHOD_win32(void)
+DSO_METHOD *DSO_METHOD_openssl(void)
{
- return (&dso_meth_win32);
+ return &dso_meth_win32;
}
/*
ERR_add_error_data(3, "filename(", filename, ")");
goto err;
}
- p = OPENSSL_malloc(sizeof(HINSTANCE));
+ p = OPENSSL_malloc(sizeof(*p));
if (p == NULL) {
DSOerr(DSO_F_WIN32_LOAD, ERR_R_MALLOC_FAILURE);
goto err;
}
/* Success */
dso->loaded_filename = filename;
- return (1);
+ return 1;
err:
/* Cleanup ! */
OPENSSL_free(filename);
OPENSSL_free(p);
if (h != NULL)
FreeLibrary(h);
- return (0);
+ return 0;
}
static int win32_unload(DSO *dso)
HINSTANCE *p;
if (dso == NULL) {
DSOerr(DSO_F_WIN32_UNLOAD, ERR_R_PASSED_NULL_PARAMETER);
- return (0);
+ return 0;
}
if (sk_void_num(dso->meth_data) < 1)
- return (1);
+ return 1;
p = sk_void_pop(dso->meth_data);
if (p == NULL) {
DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_NULL_HANDLE);
- return (0);
+ return 0;
}
if (!FreeLibrary(*p)) {
DSOerr(DSO_F_WIN32_UNLOAD, DSO_R_UNLOAD_FAILED);
* We should push the value back onto the stack in case of a retry.
*/
sk_void_push(dso->meth_data, p);
- return (0);
+ return 0;
}
/* Cleanup */
OPENSSL_free(p);
- return (1);
-}
-
-/*
- * Using GetProcAddress for variables? TODO: Check this out in the Win32 API
- * docs, there's probably a variant for variables.
- */
-static void *win32_bind_var(DSO *dso, const char *symname)
-{
- HINSTANCE *ptr;
- void *sym;
-
- if ((dso == NULL) || (symname == NULL)) {
- DSOerr(DSO_F_WIN32_BIND_VAR, ERR_R_PASSED_NULL_PARAMETER);
- return (NULL);
- }
- if (sk_void_num(dso->meth_data) < 1) {
- DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_STACK_ERROR);
- return (NULL);
- }
- ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
- if (ptr == NULL) {
- DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_NULL_HANDLE);
- return (NULL);
- }
- sym = GetProcAddress(*ptr, symname);
- if (sym == NULL) {
- DSOerr(DSO_F_WIN32_BIND_VAR, DSO_R_SYM_FAILURE);
- ERR_add_error_data(3, "symname(", symname, ")");
- return (NULL);
- }
- return (sym);
+ return 1;
}
static DSO_FUNC_TYPE win32_bind_func(DSO *dso, const char *symname)
{
HINSTANCE *ptr;
- void *sym;
+ union {
+ void *p;
+ FARPROC f;
+ } sym;
if ((dso == NULL) || (symname == NULL)) {
DSOerr(DSO_F_WIN32_BIND_FUNC, ERR_R_PASSED_NULL_PARAMETER);
- return (NULL);
+ return NULL;
}
if (sk_void_num(dso->meth_data) < 1) {
DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_STACK_ERROR);
- return (NULL);
+ return NULL;
}
ptr = sk_void_value(dso->meth_data, sk_void_num(dso->meth_data) - 1);
if (ptr == NULL) {
DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_NULL_HANDLE);
- return (NULL);
+ return NULL;
}
- sym = GetProcAddress(*ptr, symname);
- if (sym == NULL) {
+ sym.f = GetProcAddress(*ptr, symname);
+ if (sym.p == NULL) {
DSOerr(DSO_F_WIN32_BIND_FUNC, DSO_R_SYM_FAILURE);
ERR_add_error_data(3, "symname(", symname, ")");
- return (NULL);
+ return NULL;
}
- return ((DSO_FUNC_TYPE)sym);
+ return (DSO_FUNC_TYPE)sym.f;
}
struct file_st {
if (!filename) {
DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_NO_FILENAME);
- /*
- * goto err;
- */
- return (NULL);
+ return NULL;
}
- result = OPENSSL_malloc(sizeof(struct file_st));
+ result = OPENSSL_zalloc(sizeof(*result));
if (result == NULL) {
DSOerr(DSO_F_WIN32_SPLITTER, ERR_R_MALLOC_FAILURE);
- return (NULL);
+ return NULL;
}
- memset(result, 0, sizeof(struct file_st));
position = IN_DEVICE;
if ((filename[0] == '\\' && filename[1] == '\\')
case ':':
if (position != IN_DEVICE) {
DSOerr(DSO_F_WIN32_SPLITTER, DSO_R_INCORRECT_FILE_SYNTAX);
- /*
- * goto err;
- */
OPENSSL_free(result);
- return (NULL);
+ return NULL;
}
result->device = start;
result->devicelen = (int)(filename - start);
if (!result->filelen)
result->file = NULL;
- return (result);
+ return result;
}
static char *win32_joiner(DSO *dso, const struct file_st *file_split)
if (!file_split) {
DSOerr(DSO_F_WIN32_JOINER, ERR_R_PASSED_NULL_PARAMETER);
- return (NULL);
+ return NULL;
}
if (file_split->node) {
len += 2 + file_split->nodelen; /* 2 for starting \\ */
if (!len) {
DSOerr(DSO_F_WIN32_JOINER, DSO_R_EMPTY_FILE_STRUCTURE);
- return (NULL);
+ return NULL;
}
result = OPENSSL_malloc(len + 1);
- if (!result) {
+ if (result == NULL) {
DSOerr(DSO_F_WIN32_JOINER, ERR_R_MALLOC_FAILURE);
- return (NULL);
+ return NULL;
}
if (file_split->node) {
strncpy(&result[offset], file_split->file, file_split->filelen);
offset += file_split->filelen;
result[offset] = '\0';
- return (result);
+ return result;
}
static char *win32_merger(DSO *dso, const char *filespec1,
if (!filespec1 && !filespec2) {
DSOerr(DSO_F_WIN32_MERGER, ERR_R_PASSED_NULL_PARAMETER);
- return (NULL);
+ return NULL;
}
if (!filespec2) {
- merged = OPENSSL_malloc(strlen(filespec1) + 1);
- if (!merged) {
+ merged = OPENSSL_strdup(filespec1);
+ if (merged == NULL) {
DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
- return (NULL);
+ return NULL;
}
- strcpy(merged, filespec1);
} else if (!filespec1) {
- merged = OPENSSL_malloc(strlen(filespec2) + 1);
- if (!merged) {
+ merged = OPENSSL_strdup(filespec2);
+ if (merged == NULL) {
DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
- return (NULL);
+ return NULL;
}
- strcpy(merged, filespec2);
} else {
filespec1_split = win32_splitter(dso, filespec1, 0);
if (!filespec1_split) {
DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
- return (NULL);
+ return NULL;
}
filespec2_split = win32_splitter(dso, filespec2, 1);
if (!filespec2_split) {
DSOerr(DSO_F_WIN32_MERGER, ERR_R_MALLOC_FAILURE);
OPENSSL_free(filespec1_split);
- return (NULL);
+ return NULL;
}
/* Fill in into filespec1_split */
}
OPENSSL_free(filespec1_split);
OPENSSL_free(filespec2_split);
- return (merged);
+ return merged;
}
static char *win32_name_converter(DSO *dso, const char *filename)
translated = OPENSSL_malloc(len + 1);
if (translated == NULL) {
DSOerr(DSO_F_WIN32_NAME_CONVERTER, DSO_R_NAME_TRANSLATION_FAILED);
- return (NULL);
+ return NULL;
}
if (transform)
sprintf(translated, "%s.dll", filename);
else
sprintf(translated, "%s", filename);
- return (translated);
+ return translated;
}
static const char *openssl_strnchr(const char *string, int c, size_t len)
module_first = (MODULE32) GetProcAddress(dll, "Module32First");
module_next = (MODULE32) GetProcAddress(dll, "Module32Next");
+ /*
+ * Take a snapshot of current process which includes
+ * list of all involved modules.
+ */
hModuleSnap = (*create_snap) (TH32CS_SNAPMODULE, 0);
if (hModuleSnap == INVALID_HANDLE_VALUE) {
FreeLibrary(dll);
return -1;
}
+ /* Enumerate the modules to find one which includes me. */
do {
- if ((BYTE *) addr >= me32.modBaseAddr &&
- (BYTE *) addr < me32.modBaseAddr + me32.modBaseSize) {
+ if ((uintptr_t) addr >= (uintptr_t) me32.modBaseAddr &&
+ (uintptr_t) addr < (uintptr_t) (me32.modBaseAddr + me32.modBaseSize)) {
(*close_snap) (hModuleSnap);
FreeLibrary(dll);
# ifdef _WIN32_WCE
len = sz - 1;
for (i = 0; i < len; i++)
path[i] = (char)me32.szExePath[i];
- path[len++] = 0;
+ path[len++] = '\0';
return len;
}
# endif
if (len >= sz)
len = sz - 1;
memcpy(path, me32.szExePath, len);
- path[len++] = 0;
+ path[len++] = '\0';
return len;
}
# endif
CREATETOOLHELP32SNAPSHOT create_snap;
CLOSETOOLHELP32SNAPSHOT close_snap;
MODULE32 module_first, module_next;
- FARPROC ret = NULL;
+ union {
+ void *p;
+ FARPROC f;
+ } ret = { NULL };
dll = LoadLibrary(TEXT(DLLNAME));
if (dll == NULL) {
}
do {
- if ((ret = GetProcAddress(me32.hModule, name))) {
+ if ((ret.f = GetProcAddress(me32.hModule, name))) {
(*close_snap) (hModuleSnap);
FreeLibrary(dll);
- return ret;
+ return ret.p;
}
} while ((*module_next) (hModuleSnap, &me32));