From 341ee79a01307dd15a2f3b883745fa6c5072e033 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 10 Nov 2016 01:28:22 +1100 Subject: Start of windows support Only thing not working with this commit is inet_pton/inet_ntop --- src/openssl.c | 86 ++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 5507762..b51ae03 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -40,15 +40,27 @@ #include /* ssize_t pid_t */ #include /* struct timeval gettimeofday(2) */ #include /* struct stat stat(2) */ +#ifdef _WIN32 +#include /* AF_INET AF_INET6 */ +#include /* struct in_addr struct in6_addr */ +#include /* inet_pton(3) */ +#pragma comment(lib, "ws2_32.lib") +#else #include /* AF_INET AF_INET6 */ -#include /* RUSAGE_SELF struct rusage getrusage(2) */ -#include /* struct utsname uname(3) */ -#include /* O_RDONLY O_CLOEXEC open(2) */ -#include /* close(2) getpid(2) */ #include /* struct in_addr struct in6_addr */ #include /* inet_pton(3) */ +#endif +#include /* O_RDONLY O_CLOEXEC open(2) */ +#include /* close(2) getpid(2) */ #include /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ +#ifdef __WIN32 +#define EXPORT __declspec (dllexport) +#else +#include /* RUSAGE_SELF struct rusage getrusage(2) */ +#include /* struct utsname uname(3) */ #include /* dladdr(3) dlopen(3) */ +#define EXPORT +#endif #if __APPLE__ #include /* mach_absolute_time() */ @@ -797,7 +809,13 @@ static const char *aux_strerror_r(int error, char *dst, size_t lim) { static const char unknown[] = "Unknown error: "; size_t n; -#if STRERROR_R_CHAR_P +#if _WIN32 + char *rv = strerror(error); + n = MIN(strlen(rv) - 1, lim); + memcpy(dst, rv, n); + return dst; + +#elif STRERROR_R_CHAR_P char *rv = strerror_r(error, dst, lim); if (rv != NULL) @@ -1278,10 +1296,12 @@ static const char *auxL_pusherror(lua_State *L, int error, const char *fun) { } else { return lua_pushfstring(L, "%s:%d:%s", file, line, txt); } +#if HAVE_DLADDR } else if (error == auxL_EDYLD) { const char *const fmt = (fun)? "%s: %s" : "%.0s%s"; return lua_pushfstring(L, fmt, (fun)? fun : "", dlerror()); +#endif } else { const char *const fmt = (fun)? "%s: %s" : "%.0s%s"; @@ -2286,7 +2306,7 @@ static void initall(lua_State *L); * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ -int luaopen__openssl_compat(lua_State *L) { +EXPORT int luaopen__openssl_compat(lua_State *L) { initall(L); lua_newtable(L); @@ -2483,7 +2503,7 @@ static const auxL_IntegerReg ssleay_version[] = { { NULL, 0 }, }; -int luaopen__openssl(lua_State *L) { +EXPORT int luaopen__openssl(lua_State *L) { size_t i; auxL_newlib(L, ossl_globals, 0); @@ -3150,7 +3170,7 @@ static const auxL_Reg bn_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_bignum(lua_State *L) { +EXPORT int luaopen__openssl_bignum(lua_State *L) { initall(L); auxL_newlib(L, bn_globals, 0); @@ -4499,7 +4519,7 @@ static const auxL_IntegerReg pk_rsa_pad_opts[] = { { NULL, 0 }, }; -int luaopen__openssl_pkey(lua_State *L) { +EXPORT int luaopen__openssl_pkey(lua_State *L) { initall(L); auxL_newlib(L, pk_globals, 0); @@ -4512,7 +4532,7 @@ int luaopen__openssl_pkey(lua_State *L) { /* * Deprecated module name. */ -int luaopen__openssl_pubkey(lua_State *L) { +EXPORT int luaopen__openssl_pubkey(lua_State *L) { return luaopen__openssl_pkey(L); } /* luaopen__openssl_pubkey() */ @@ -4688,7 +4708,7 @@ static const auxL_Reg ecg_globals[] = { #endif /* OPENSSL_NO_EC */ -int luaopen__openssl_ec_group(lua_State *L) { +EXPORT int luaopen__openssl_ec_group(lua_State *L) { #ifndef OPENSSL_NO_EC initall(L); @@ -4895,7 +4915,7 @@ static const auxL_Reg xn_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_name(lua_State *L) { +EXPORT int luaopen__openssl_x509_name(lua_State *L) { initall(L); auxL_newlib(L, xn_globals, 0); @@ -5149,7 +5169,7 @@ static const auxL_Reg gn_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_altname(lua_State *L) { +EXPORT int luaopen__openssl_x509_altname(lua_State *L) { initall(L); auxL_newlib(L, gn_globals, 0); @@ -5451,7 +5471,7 @@ static const auxL_IntegerReg xe_textopts[] = { { NULL, 0 }, }; -int luaopen__openssl_x509_extension(lua_State *L) { +EXPORT int luaopen__openssl_x509_extension(lua_State *L) { initall(L); auxL_newlib(L, xe_globals, 0); @@ -6507,7 +6527,7 @@ static const auxL_Reg xc_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_cert(lua_State *L) { +EXPORT int luaopen__openssl_x509_cert(lua_State *L) { initall(L); auxL_newlib(L, xc_globals, 0); @@ -6814,7 +6834,7 @@ static const auxL_Reg xr_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_csr(lua_State *L) { +EXPORT int luaopen__openssl_x509_csr(lua_State *L) { initall(L); auxL_newlib(L, xr_globals, 0); @@ -7233,7 +7253,7 @@ static const auxL_Reg xx_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_crl(lua_State *L) { +EXPORT int luaopen__openssl_x509_crl(lua_State *L) { initall(L); auxL_newlib(L, xx_globals, 0); @@ -7391,7 +7411,7 @@ static const auxL_Reg xl_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_chain(lua_State *L) { +EXPORT int luaopen__openssl_x509_chain(lua_State *L) { initall(L); auxL_newlib(L, xl_globals, 0); @@ -7585,7 +7605,7 @@ static const auxL_Reg xs_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_store(lua_State *L) { +EXPORT int luaopen__openssl_x509_store(lua_State *L) { initall(L); auxL_newlib(L, xs_globals, 0); @@ -7664,7 +7684,7 @@ static const auxL_Reg stx_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_x509_store_context(lua_State *L) { +EXPORT int luaopen__openssl_x509_store_context(lua_State *L) { initall(L); auxL_newlib(L, stx_globals, 0); @@ -7837,7 +7857,7 @@ static const auxL_Reg p12_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_pkcs12(lua_State *L) { +EXPORT int luaopen__openssl_pkcs12(lua_State *L) { initall(L); auxL_newlib(L, p12_globals, 0); @@ -8550,7 +8570,7 @@ static const auxL_IntegerReg sx_option[] = { { NULL, 0 }, }; -int luaopen__openssl_ssl_context(lua_State *L) { +EXPORT int luaopen__openssl_ssl_context(lua_State *L) { initall(L); auxL_newlib(L, sx_globals, 0); @@ -9107,7 +9127,7 @@ static const auxL_IntegerReg ssl_version[] = { }; -int luaopen__openssl_ssl(lua_State *L) { +EXPORT int luaopen__openssl_ssl(lua_State *L) { initall(L); auxL_newlib(L, ssl_globals, 0); @@ -9375,7 +9395,7 @@ static const auxL_IntegerReg xp_inherit_flags[] = { { NULL, 0 } }; -int luaopen__openssl_x509_verify_param(lua_State *L) { +EXPORT int luaopen__openssl_x509_verify_param(lua_State *L) { initall(L); auxL_newlib(L, xp_globals, 0); @@ -9487,7 +9507,7 @@ static const auxL_Reg md_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_digest(lua_State *L) { +EXPORT int luaopen__openssl_digest(lua_State *L) { initall(L); auxL_newlib(L, md_globals, 0); @@ -9599,7 +9619,7 @@ static const auxL_Reg hmac_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_hmac(lua_State *L) { +EXPORT int luaopen__openssl_hmac(lua_State *L) { initall(L); auxL_newlib(L, hmac_globals, 0); @@ -9806,7 +9826,7 @@ static const auxL_Reg cipher_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_cipher(lua_State *L) { +EXPORT int luaopen__openssl_cipher(lua_State *L) { initall(L); auxL_newlib(L, cipher_globals, 0); @@ -9893,7 +9913,7 @@ static const auxL_Reg or_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_ocsp_response(lua_State *L) { +EXPORT int luaopen__openssl_ocsp_response(lua_State *L) { initall(L); auxL_newlib(L, or_globals, 0); @@ -9965,7 +9985,7 @@ static const auxL_IntegerReg ob_verify_flags[] = { { NULL, 0 }, }; -int luaopen__openssl_ocsp_basic(lua_State *L) { +EXPORT int luaopen__openssl_ocsp_basic(lua_State *L) { initall(L); auxL_newlib(L, ob_globals, 0); @@ -10091,8 +10111,10 @@ error:; struct { struct timeval tv; pid_t pid; +#ifndef _WIN32 struct rusage ru; struct utsname un; +#endif uintptr_t aslr; #if defined __APPLE__ uint64_t mt; @@ -10103,8 +10125,10 @@ error:; gettimeofday(&junk.tv, NULL); junk.pid = getpid(); +#ifndef _WIN32 getrusage(RUSAGE_SELF, &junk.ru); uname(&junk.un); +#endif junk.aslr = (uintptr_t)&strcpy ^ (uintptr_t)&randL_stir; #if defined __APPLE__ junk.mt = mach_absolute_time(); @@ -10316,7 +10340,7 @@ static const auxL_Reg rand_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_rand(lua_State *L) { +EXPORT int luaopen__openssl_rand(lua_State *L) { struct randL_state *st; initall(L); @@ -10364,7 +10388,7 @@ static const auxL_Reg des_globals[] = { { NULL, NULL }, }; -int luaopen__openssl_des(lua_State *L) { +EXPORT int luaopen__openssl_des(lua_State *L) { initall(L); auxL_newlib(L, des_globals, 0); -- cgit v1.2.3-59-g8ed1b From 5c8fd3906e6bac0914c6968a33719cc1bf5cdbf0 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 00:16:32 +1000 Subject: Implement dl_anchor for win32 --- src/openssl.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/openssl.c b/src/openssl.c index b51ae03..9c87bc9 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1342,7 +1342,15 @@ static const EVP_MD *auxL_optdigest(lua_State *L, int index, EVP_PKEY *key, cons */ /* dl_anchor must not be called from multiple threads at once */ static int dl_anchor(void) { -#if HAVE_DLADDR +#if _WIN32 + EXPORT extern int luaopen__openssl(lua_State *); + + HMODULE dummy; + if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_PIN|GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (void *)&luaopen__openssl, &dummy)) + return GetLastError(); + + return 0; +#elif HAVE_DLADDR extern int luaopen__openssl(lua_State *); static void *anchor; Dl_info info; -- cgit v1.2.3-59-g8ed1b From 328b61f0730a544200538945badb927274c0bd2b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 00:29:45 +1000 Subject: Add win32 implementation of locking --- src/openssl.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 9c87bc9..92f372c 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -52,10 +52,10 @@ #endif #include /* O_RDONLY O_CLOEXEC open(2) */ #include /* close(2) getpid(2) */ -#include /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ #ifdef __WIN32 #define EXPORT __declspec (dllexport) #else +#include /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ #include /* RUSAGE_SELF struct rusage getrusage(2) */ #include /* struct utsname uname(3) */ #include /* dladdr(3) dlopen(3) */ @@ -10414,15 +10414,27 @@ EXPORT int luaopen__openssl_des(lua_State *L) { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ static struct { +#if _WIN32 + HANDLE *lock; +#else pthread_mutex_t *lock; +#endif int nlock; } mt_state; static void mt_lock(int mode, int type, const char *file NOTUSED, int line NOTUSED) { if (mode & CRYPTO_LOCK) +#if _WIN32 + WaitForSingleObject(mt_state.lock[type], INFINITE); +#else pthread_mutex_lock(&mt_state.lock[type]); +#endif else +#if _WIN32 + ReleaseMutex(mt_state.lock[type]); +#else pthread_mutex_unlock(&mt_state.lock[type]); +#endif } /* mt_lock() */ /* @@ -10448,6 +10460,8 @@ static unsigned long mt_gettid(void) { return id; #elif __NetBSD__ return _lwp_self(); +#elif _WIN32 + return GetCurrentThreadId(); #else /* * pthread_t is an integer on Solaris and Linux, an unsigned integer @@ -10477,9 +10491,18 @@ static int mt_init(void) { } for (i = 0; i < mt_state.nlock; i++) { +#if _WIN32 + if (!(mt_state.lock[i] = CreateMutex(NULL, FALSE, NULL))) { + error = GetLastError(); +#else if ((error = pthread_mutex_init(&mt_state.lock[i], NULL))) { +#endif while (i > 0) { +#if _WIN32 + CloseHandle(mt_state.lock[--i]); +#else pthread_mutex_destroy(&mt_state.lock[--i]); +#endif } free(mt_state.lock); @@ -10511,11 +10534,24 @@ epilog: static void initall(lua_State *L) { - static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static int initssl; int error = 0; +#if _WIN32 + static volatile HANDLE mutex = NULL; + if (mutex == NULL) { + HANDLE p; + if (!(p = CreateMutex(NULL, FALSE, NULL))) + auxL_error(L, GetLastError(), "openssl.init"); + if (InterlockedCompareExchangePointer((PVOID*)&mutex, (PVOID)p, NULL) != NULL) + CloseHandle(p); + } + if (WaitForSingleObject(mutex, INFINITE) == WAIT_FAILED) + auxL_error(L, GetLastError(), "openssl.init"); +#else + static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_lock(&mutex); +#endif #if !OPENSSL_PREREQ(1,1,0) if (!error) @@ -10542,7 +10578,11 @@ static void initall(lua_State *L) { if (!error) error = ex_init(); +#if _WIN32 + ReleaseMutex(mutex); +#else pthread_mutex_unlock(&mutex); +#endif if (error) auxL_error(L, error, "openssl.init"); -- cgit v1.2.3-59-g8ed1b From fbb50a8888ce145e32b15a499c0c189bd51d94d3 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 01:39:32 +1000 Subject: Implement randL_stir for win32, reorder includes --- src/openssl.c | 89 +++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 68 insertions(+), 21 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 92f372c..6e30726 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -37,28 +37,29 @@ #include /* ENOMEM ENOTSUP EOVERFLOW errno */ #include /* assert */ -#include /* ssize_t pid_t */ -#include /* struct timeval gettimeofday(2) */ #include /* struct stat stat(2) */ #ifdef _WIN32 -#include /* AF_INET AF_INET6 */ -#include /* struct in_addr struct in6_addr */ -#include /* inet_pton(3) */ +#include /* struct in_addr, struct in6_addr */ +#include /* AF_INET, AF_INET6 */ +#include /* inet_pton */ #pragma comment(lib, "ws2_32.lib") +#include /* CryptAcquireContext(), CryptGenRandom(), CryptReleaseContext() */ +#pragma comment(lib, "advapi32.lib") +#include /* CreateMutex(), GetLastError(), GetModuleHandleEx(), GetProcessTimes(), InterlockedCompareExchangePointer() */ +#pragma comment(lib, "kernel32.lib") +#define EXPORT __declspec (dllexport) #else -#include /* AF_INET AF_INET6 */ -#include /* struct in_addr struct in6_addr */ #include /* inet_pton(3) */ -#endif +#include /* dladdr(3) dlopen(3) */ #include /* O_RDONLY O_CLOEXEC open(2) */ -#include /* close(2) getpid(2) */ -#ifdef __WIN32 -#define EXPORT __declspec (dllexport) -#else +#include /* struct in_addr struct in6_addr */ #include /* pthread_mutex_init(3) pthread_mutex_lock(3) pthread_mutex_unlock(3) */ #include /* RUSAGE_SELF struct rusage getrusage(2) */ +#include /* AF_INET AF_INET6 */ +#include /* struct timeval gettimeofday(2) */ +#include /* ssize_t pid_t */ #include /* struct utsname uname(3) */ -#include /* dladdr(3) dlopen(3) */ +#include /* close(2) getpid(2) */ #define EXPORT #endif @@ -10009,7 +10010,11 @@ EXPORT int luaopen__openssl_ocsp_basic(lua_State *L) { * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ struct randL_state { +#ifdef _WIN32 + DWORD pid; +#else pid_t pid; +#endif }; /* struct randL_state */ static struct randL_state *randL_getstate(lua_State *L) { @@ -10029,6 +10034,31 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { int error; unsigned char data[256]; +#ifdef _WIN32 + HCRYPTPROV hCryptProv; + BOOL ok; + + if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { + error = GetLastError(); + goto error; + } + while (count < rqstd) { + ok = CryptGenRandom(hCryptProv, sizeof data, (BYTE*)data); + if (!ok) { + CryptReleaseContext(hCryptProv, 0); + error = GetLastError(); + goto error; + } + + RAND_seed(data, sizeof data); + + count += sizeof data; + } + + CryptReleaseContext(hCryptProv, 0); + + st->pid = GetCurrentProcessId(); +#else #if HAVE_ARC4RANDOM_BUF while (count < rqstd) { size_t n = MIN(rqstd - count, sizeof data); @@ -10080,8 +10110,10 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { int fd = open("/dev/urandom", O_RDONLY); #endif - if (fd == -1) - goto syserr; + if (fd == -1) { + error = errno; + goto error; + } while (count < rqstd) { ssize_t n = read(fd, data, MIN(rqstd - count, sizeof data)); @@ -10111,15 +10143,18 @@ static int randL_stir(struct randL_state *st, unsigned rqstd) { } st->pid = getpid(); +#endif /* _WIN32 */ return 0; -syserr: - error = errno; error:; struct { - struct timeval tv; +#ifdef _WIN32 + DWORD pid; + SYSTEMTIME tv; + FILETIME ftCreation, ftExit, ftKernel, ftUser; +#else pid_t pid; -#ifndef _WIN32 + struct timeval tv; struct rusage ru; struct utsname un; #endif @@ -10131,9 +10166,13 @@ error:; #endif } junk; - gettimeofday(&junk.tv, NULL); +#ifdef _WIN32 + junk.pid = GetCurrentProcessId(); + GetSystemTime(&junk.tv); + GetProcessTimes(GetCurrentProcess(), &junk.ftCreation, &junk.ftExit, &junk.ftKernel, &junk.ftUser); +#else junk.pid = getpid(); -#ifndef _WIN32 + gettimeofday(&junk.tv, NULL); getrusage(RUSAGE_SELF, &junk.ru); uname(&junk.un); #endif @@ -10158,14 +10197,22 @@ error:; RAND_add(&junk, sizeof junk, 0.1); +#ifdef _WIN32 + st->pid = GetCurrentProcessId(); +#else st->pid = getpid(); +#endif return error; } /* randL_stir() */ static void randL_checkpid(struct randL_state *st) { +#ifdef _WIN32 + if (st->pid != GetCurrentProcessId()) +#else if (st->pid != getpid()) +#endif (void)randL_stir(st, 16); } /* randL_checkpid() */ -- cgit v1.2.3-59-g8ed1b From 912438dde5babdd04acf062d9aef45fa0ac1c1c2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 01:54:53 +1000 Subject: Implement a thread-safe strerror for windows --- src/openssl.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 6e30726..5c47635 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -811,11 +811,10 @@ static const char *aux_strerror_r(int error, char *dst, size_t lim) { size_t n; #if _WIN32 - char *rv = strerror(error); - n = MIN(strlen(rv) - 1, lim); - memcpy(dst, rv, n); - return dst; + errno_t rv = strerror_s(dst, lim, error); + if (rv) + return dst; #elif STRERROR_R_CHAR_P char *rv = strerror_r(error, dst, lim); -- cgit v1.2.3-59-g8ed1b From 8949ef3cf68bb5038cfffefcb2fed0c2c30180d8 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 02:13:10 +1000 Subject: windows doesn't have strings.h for strcasecmp, however it does have _stricmp --- src/openssl.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 5c47635..b9aaeb2 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -30,7 +30,6 @@ #include /* INT_MAX INT_MIN LLONG_MAX LLONG_MIN UCHAR_MAX ULLONG_MAX */ #include /* uintptr_t */ #include /* memset(3) strerror_r(3) */ -#include /* strcasecmp(3) */ #include /* INFINITY fabs(3) floor(3) frexp(3) fmod(3) round(3) isfinite(3) */ #include /* struct tm time_t strptime(3) time(2) */ #include /* isdigit(3), isxdigit(3), tolower(3) */ @@ -493,7 +492,12 @@ #undef MIN #define MIN(a, b) (((a) < (b))? (a) : (b)) +#ifdef _WIN32 +#define stricmp(a, b) _stricmp((a), (b)) +#else +#include /* strcasecmp(3) */ #define stricmp(a, b) strcasecmp((a), (b)) +#endif #define strieq(a, b) (!stricmp((a), (b))) #define xtolower(c) tolower((unsigned char)(c)) @@ -988,7 +992,13 @@ NOTUSED static auxtype_t auxL_getref(lua_State *L, auxref_t ref) { static int auxL_testoption(lua_State *L, int index, const char *def, const char *const *optlist, _Bool nocase) { const char *optname = (def)? luaL_optstring(L, index, def) : luaL_checkstring(L, index); - int (*optcmp)() = (nocase)? &strcasecmp : &strcmp; + int (*optcmp)() = (nocase)? +#ifdef _WIN32 + &_stricmp +#else + &strcasecmp +#endif + : &strcmp; int i; for (i = 0; optlist[i]; i++) { -- cgit v1.2.3-59-g8ed1b From 66acd9c9075bdb9160cf0e30f7128e62bf576f5f Mon Sep 17 00:00:00 2001 From: daurnimator Date: Tue, 4 Apr 2017 02:49:49 +1000 Subject: EXPAND macro for MSVC See http://stackoverflow.com/questions/32399191/va-args-expansion-using-msvc --- src/openssl.c | 55 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index b9aaeb2..ede55aa 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -569,8 +569,9 @@ static void *prepsimple(lua_State *L, const char *tname, int (*gc)(lua_State *)) return p; } /* prepsimple() */ +#define EXPAND( x ) x #define prepsimple_(a, b, c, ...) prepsimple((a), (b), (c)) -#define prepsimple(...) prepsimple_(__VA_ARGS__, 0, 0) +#define prepsimple(...) EXPAND( prepsimple_(__VA_ARGS__, 0, 0) ) static void *checksimple(lua_State *L, int index, const char *tname) { @@ -1061,7 +1062,7 @@ static void auxL_pushunsigned(lua_State *L, auxL_Unsigned i) { } /* auxL_pushunsigned() */ #define auxL_checkinteger_(a, b, c, d, ...) auxL_checkinteger((a), (b), (c), (d)) -#define auxL_checkinteger(...) auxL_checkinteger_(__VA_ARGS__, auxL_IntegerMin, auxL_IntegerMax, 0) +#define auxL_checkinteger(...) EXPAND( auxL_checkinteger_(__VA_ARGS__, auxL_IntegerMin, auxL_IntegerMax, 0) ) static auxL_Integer (auxL_checkinteger)(lua_State *L, int index, auxL_Integer min, auxL_Integer max) { auxL_Integer i; @@ -1080,14 +1081,14 @@ static auxL_Integer (auxL_checkinteger)(lua_State *L, int index, auxL_Integer mi } /* auxL_checkinteger() */ #define auxL_optinteger_(a, b, c, d, e, ...) auxL_optinteger((a), (b), (c), (d), (e)) -#define auxL_optinteger(...) auxL_optinteger_(__VA_ARGS__, auxL_IntegerMin, auxL_IntegerMax, 0) +#define auxL_optinteger(...) EXPAND( auxL_optinteger_(__VA_ARGS__, auxL_IntegerMin, auxL_IntegerMax, 0)) static auxL_Integer (auxL_optinteger)(lua_State *L, int index, auxL_Integer def, auxL_Integer min, auxL_Integer max) { return (lua_isnoneornil(L, index))? def : auxL_checkinteger(L, index, min, max); } /* auxL_optinteger() */ #define auxL_checkunsigned_(a, b, c, d, ...) auxL_checkunsigned((a), (b), (c), (d)) -#define auxL_checkunsigned(...) auxL_checkunsigned_(__VA_ARGS__, auxL_UnsignedMin, auxL_UnsignedMax, 0) +#define auxL_checkunsigned(...) EXPAND( auxL_checkunsigned_(__VA_ARGS__, auxL_UnsignedMin, auxL_UnsignedMax, 0)) static auxL_Unsigned (auxL_checkunsigned)(lua_State *L, int index, auxL_Unsigned min, auxL_Unsigned max) { auxL_Unsigned i; @@ -1107,7 +1108,7 @@ static auxL_Unsigned (auxL_checkunsigned)(lua_State *L, int index, auxL_Unsigned } /* auxL_checkunsigned() */ #define auxL_optunsigned_(a, b, c, d, e, ...) auxL_optunsigned((a), (b), (c), (d), (e)) -#define auxL_optunsigned(...) auxL_optunsigned_(__VA_ARGS__, auxL_UnsignedMin, auxL_UnsignedMax, 0) +#define auxL_optunsigned(...) EXPAND( auxL_optunsigned_(__VA_ARGS__, auxL_UnsignedMin, auxL_UnsignedMax, 0) ) static auxL_Unsigned (auxL_optunsigned)(lua_State *L, int index, auxL_Unsigned def, auxL_Unsigned min, auxL_Unsigned max) { return (lua_isnoneornil(L, index))? def : auxL_checkunsigned(L, index, min, max); @@ -1231,7 +1232,7 @@ static _Bool auxL_newclass(lua_State *L, const char *name, const auxL_Reg *metho } /* auxL_newclass() */ #define auxL_addclass(L, ...) \ - (auxL_newclass((L), __VA_ARGS__), lua_pop((L), 1)) + EXPAND( (auxL_newclass((L), __VA_ARGS__), lua_pop((L), 1)) ) static int auxL_swaptable(lua_State *L, int index) { index = lua_absindex(L, index); @@ -1411,7 +1412,7 @@ static struct { #endif #if !HAVE_DH_GET0_KEY -#define DH_get0_key(...) compat_DH_get0_key(__VA_ARGS__) +#define DH_get0_key(...) EXPAND( compat_DH_get0_key(__VA_ARGS__) ) static void compat_DH_get0_key(const DH *d, const BIGNUM **pub_key, const BIGNUM **priv_key) { if (pub_key) @@ -1422,7 +1423,7 @@ static void compat_DH_get0_key(const DH *d, const BIGNUM **pub_key, const BIGNUM #endif #if !HAVE_DH_GET0_PQG -#define DH_get0_pqg(...) compat_DH_get0_pqg(__VA_ARGS__) +#define DH_get0_pqg(...) EXPAND( compat_DH_get0_pqg(__VA_ARGS__) ) static void compat_DH_get0_pqg(const DH *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) { if (p) @@ -1435,7 +1436,7 @@ static void compat_DH_get0_pqg(const DH *d, const BIGNUM **p, const BIGNUM **q, #endif #if !HAVE_DH_SET0_KEY -#define DH_set0_key(...) compat_DH_set0_key(__VA_ARGS__) +#define DH_set0_key(...) EXPAND( compat_DH_set0_key(__VA_ARGS__) ) static void compat_DH_set0_key(DH *d, BIGNUM *pub_key, BIGNUM *priv_key) { if (pub_key) @@ -1446,7 +1447,7 @@ static void compat_DH_set0_key(DH *d, BIGNUM *pub_key, BIGNUM *priv_key) { #endif #if !HAVE_DH_SET0_PQG -#define DH_set0_pqg(...) compat_DH_set0_pqg(__VA_ARGS__) +#define DH_set0_pqg(...) EXPAND( compat_DH_set0_pqg(__VA_ARGS__) ) static void compat_DH_set0_pqg(DH *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) { if (p) @@ -1459,7 +1460,7 @@ static void compat_DH_set0_pqg(DH *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) { #endif #if !HAVE_DSA_GET0_KEY -#define DSA_get0_key(...) compat_DSA_get0_key(__VA_ARGS__) +#define DSA_get0_key(...) EXPAND( compat_DSA_get0_key(__VA_ARGS__) ) static void compat_DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key) { if (pub_key) @@ -1470,7 +1471,7 @@ static void compat_DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGN #endif #if !HAVE_DSA_GET0_PQG -#define DSA_get0_pqg(...) compat_DSA_get0_pqg(__VA_ARGS__) +#define DSA_get0_pqg(...) EXPAND( compat_DSA_get0_pqg(__VA_ARGS__) ) static void compat_DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g) { if (p) @@ -1483,7 +1484,7 @@ static void compat_DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q #endif #if !HAVE_DSA_SET0_KEY -#define DSA_set0_key(...) compat_DSA_set0_key(__VA_ARGS__) +#define DSA_set0_key(...) EXPAND( compat_DSA_set0_key(__VA_ARGS__) ) static void compat_DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) { if (pub_key) @@ -1494,7 +1495,7 @@ static void compat_DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key) { #endif #if !HAVE_DSA_SET0_PQG -#define DSA_set0_pqg(...) compat_DSA_set0_pqg(__VA_ARGS__) +#define DSA_set0_pqg(...) EXPAND( compat_DSA_set0_pqg(__VA_ARGS__) ) static void compat_DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g) { if (p) @@ -1552,7 +1553,7 @@ static int compat_EVP_PKEY_base_id(EVP_PKEY *key) { #if !HAVE_EVP_PKEY_GET_DEFAULT_DIGEST_NID #define EVP_PKEY_get_default_digest_nid(...) \ - compat_EVP_PKEY_get_default_digest_nid(__VA_ARGS__) + EXPAND( compat_EVP_PKEY_get_default_digest_nid(__VA_ARGS__) ) static int compat_EVP_PKEY_get_default_digest_nid(EVP_PKEY *key, int *nid) { switch (EVP_PKEY_base_id(key)) { @@ -1633,7 +1634,7 @@ static HMAC_CTX *compat_HMAC_CTX_new(void) { #endif #if !HAVE_RSA_GET0_CRT_PARAMS -#define RSA_get0_crt_params(...) compat_RSA_get0_crt_params(__VA_ARGS__) +#define RSA_get0_crt_params(...) EXPAND( compat_RSA_get0_crt_params(__VA_ARGS__) ) static void compat_RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1, const BIGNUM **iqmp) { if (dmp1) @@ -1646,7 +1647,7 @@ static void compat_RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const #endif #if !HAVE_RSA_GET0_FACTORS -#define RSA_get0_factors(...) compat_RSA_get0_factors(__VA_ARGS__) +#define RSA_get0_factors(...) EXPAND( compat_RSA_get0_factors(__VA_ARGS__) ) static void compat_RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) { if (p) @@ -1657,7 +1658,7 @@ static void compat_RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM #endif #if !HAVE_RSA_GET0_KEY -#define RSA_get0_key(...) compat_RSA_get0_key(__VA_ARGS__) +#define RSA_get0_key(...) EXPAND( compat_RSA_get0_key(__VA_ARGS__) ) static void compat_RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) { if (n) @@ -1670,7 +1671,7 @@ static void compat_RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e #endif #if !HAVE_RSA_SET0_CRT_PARAMS -#define RSA_set0_crt_params(...) compat_RSA_set0_crt_params(__VA_ARGS__) +#define RSA_set0_crt_params(...) EXPAND( compat_RSA_set0_crt_params(__VA_ARGS__) ) static void compat_RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) { if (dmp1) @@ -1683,7 +1684,7 @@ static void compat_RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNU #endif #if !HAVE_RSA_SET0_FACTORS -#define RSA_set0_factors(...) compat_RSA_set0_factors(__VA_ARGS__) +#define RSA_set0_factors(...) EXPAND( compat_RSA_set0_factors(__VA_ARGS__) ) static void compat_RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) { if (p) @@ -1694,7 +1695,7 @@ static void compat_RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) { #endif #if !HAVE_RSA_SET0_KEY -#define RSA_set0_key(...) compat_RSA_set0_key(__VA_ARGS__) +#define RSA_set0_key(...) EXPAND( compat_RSA_set0_key(__VA_ARGS__) ) static void compat_RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) { if (n) @@ -1707,7 +1708,7 @@ static void compat_RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) { #endif #if !HAVE_SSL_GET_CLIENT_RANDOM -#define SSL_get_client_random(...) compat_SSL_get_client_random(__VA_ARGS__) +#define SSL_get_client_random(...) EXPAND( compat_SSL_get_client_random(__VA_ARGS__) ) static size_t compat_SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen) { if (outlen == 0) return sizeof(ssl->s3->client_random); @@ -1719,7 +1720,7 @@ static size_t compat_SSL_get_client_random(const SSL *ssl, unsigned char *out, s #endif #if !HAVE_SSL_CLIENT_VERSION -#define SSL_client_version(...) compat_SSL_client_version(__VA_ARGS__) +#define SSL_client_version(...) EXPAND( compat_SSL_client_version(__VA_ARGS__) ) static int compat_SSL_client_version(const SSL *ssl) { return ssl->client_version; @@ -1743,7 +1744,7 @@ static int compat_SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) { #endif #if !HAVE_SSL_UP_REF -#define SSL_up_ref(...) compat_SSL_up_ref(__VA_ARGS__) +#define SSL_up_ref(...) EXPAND( compat_SSL_up_ref(__VA_ARGS__) ) static int compat_SSL_up_ref(SSL *ssl) { /* our caller should already have had a proper reference */ @@ -1909,7 +1910,7 @@ static void compat_init_X509_STORE_onfree(void *store, void *data NOTUSED, CRYPT } /* compat_init_X509_STORE_onfree() */ #if !HAVE_X509_STORE_UP_REF -#define X509_STORE_up_ref(...) compat_X509_STORE_up_ref(__VA_ARGS__) +#define X509_STORE_up_ref(...) EXPAND( compat_X509_STORE_up_ref(__VA_ARGS__) ) static int compat_X509_STORE_up_ref(X509_STORE *crt) { /* our caller should already have had a proper reference */ @@ -1921,7 +1922,7 @@ static int compat_X509_STORE_up_ref(X509_STORE *crt) { #endif #if !HAVE_X509_UP_REF -#define X509_up_ref(...) compat_X509_up_ref(__VA_ARGS__) +#define X509_up_ref(...) EXPAND( compat_X509_up_ref(__VA_ARGS__) ) static int compat_X509_up_ref(X509 *crt) { /* our caller should already have had a proper reference */ @@ -2587,7 +2588,7 @@ static BIGNUM *bn_dup_nil(lua_State *L, const BIGNUM *src) { #define checkbig_(a, b, c, ...) checkbig((a), (b), (c)) -#define checkbig(...) checkbig_(__VA_ARGS__, &(_Bool){ 0 }, 0) +#define checkbig(...) EXPAND( checkbig_(__VA_ARGS__, &(_Bool){ 0 }, 0) ) static BIGNUM *(checkbig)(lua_State *, int, _Bool *); -- cgit v1.2.3-59-g8ed1b From c72e19728a32af131d63ead8b0ad04e7c2f061d2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 2 Sep 2017 03:58:42 +1000 Subject: Windows wants inaddr.h included after winsock2.h --- src/openssl.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/openssl.c b/src/openssl.c index ede55aa..10cb1f1 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -38,8 +38,8 @@ #include /* struct stat stat(2) */ #ifdef _WIN32 -#include /* struct in_addr, struct in6_addr */ #include /* AF_INET, AF_INET6 */ +#include /* struct in_addr, struct in6_addr */ #include /* inet_pton */ #pragma comment(lib, "ws2_32.lib") #include /* CryptAcquireContext(), CryptGenRandom(), CryptReleaseContext() */ -- cgit v1.2.3-59-g8ed1b From 16f04179e269b58ca811e1e4073e9546096a3603 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 2 Sep 2017 04:00:30 +1000 Subject: Windows doesn't have S_ISDIR --- src/openssl.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/openssl.c b/src/openssl.c index 10cb1f1..726cba3 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -493,6 +493,10 @@ #define MIN(a, b) (((a) < (b))? (a) : (b)) #ifdef _WIN32 +#if !defined(S_ISDIR) && defined(_S_IFDIR) && defined(_S_IFDIR) +#define S_ISDIR(m) (((m) & _S_IFDIR) == _S_IFDIR) +#endif + #define stricmp(a, b) _stricmp((a), (b)) #else #include /* strcasecmp(3) */ -- cgit v1.2.3-59-g8ed1b From e64b1166f5b4e205002112d341be112f3154acf6 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 2 Sep 2017 18:23:46 +1000 Subject: Don't use #pragma for linker options Instead require user to pass libraries to linker via build system --- src/openssl.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/openssl.c b/src/openssl.c index 726cba3..e5f0cd4 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -41,11 +41,8 @@ #include /* AF_INET, AF_INET6 */ #include /* struct in_addr, struct in6_addr */ #include /* inet_pton */ -#pragma comment(lib, "ws2_32.lib") #include /* CryptAcquireContext(), CryptGenRandom(), CryptReleaseContext() */ -#pragma comment(lib, "advapi32.lib") #include /* CreateMutex(), GetLastError(), GetModuleHandleEx(), GetProcessTimes(), InterlockedCompareExchangePointer() */ -#pragma comment(lib, "kernel32.lib") #define EXPORT __declspec (dllexport) #else #include /* inet_pton(3) */ -- cgit v1.2.3-59-g8ed1b