From 954553f5b18cb5a69cd73ffa5c272ad0ad4c05ce Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 16 Jun 2023 13:41:48 +0200 Subject: [PATCH 1/6] Don't call wincrypt on builds older than Windows XP On runtime it will attempt to get CryptAcquireContext() from advapi32.dll but it's not there and the DLL/program containing mbedtls will not load. Signed-off-by: Steve Lhomme --- library/entropy_poll.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/entropy_poll.c b/library/entropy_poll.c index b5024c83fa..8b60ee5364 100644 --- a/library/entropy_poll.c +++ b/library/entropy_poll.c @@ -52,6 +52,7 @@ #define _WIN32_WINNT 0x0400 #endif #include +#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_WINXP */ #include int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len, @@ -76,6 +77,9 @@ int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len, return 0; } +#else /* !_WIN32_WINNT_WINXP */ +#error Entropy not available before Windows XP, use MBEDTLS_NO_PLATFORM_ENTROPY +#endif /* !_WIN32_WINNT_WINXP */ #else /* _WIN32 && !EFIX64 && !EFI32 */ /* From d50a8cc77ca0d2a82a4c5c4028c2e8b68ce97d6d Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 16 Jun 2023 13:36:25 +0200 Subject: [PATCH 2/6] Don't force the default windows version down The _WIN32_WINNT value will pick the default value for the SDK when including windows.h. Signed-off-by: Steve Lhomme --- library/entropy_poll.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/entropy_poll.c b/library/entropy_poll.c index 8b60ee5364..f90167ca82 100644 --- a/library/entropy_poll.c +++ b/library/entropy_poll.c @@ -48,9 +48,6 @@ #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) -#if !defined(_WIN32_WINNT) -#define _WIN32_WINNT 0x0400 -#endif #include #if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_WINXP */ #include From eb0f18acbc434d135e6a93c7abd6aea53e6526e8 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 16 Jun 2023 14:12:19 +0200 Subject: [PATCH 3/6] Don't use inet_pton() before Windows Vista On runtime it will attempt to get inet_pton() from ws2_32.dll but it's not there and the DLL/program containing mbedtls will not load. Signed-off-by: Steve Lhomme --- library/x509_crt.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/x509_crt.c b/library/x509_crt.c index 9b3414a49e..453389b7ef 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -2684,6 +2684,9 @@ find_parent: #elif (defined(__MINGW32__) || defined(__MINGW64__)) && _WIN32_WINNT >= 0x0600 #include #include +#else +/* inet_pton() is not supported, fallback to software version */ +#define MBEDTLS_TEST_SW_INET_PTON #endif #elif defined(__sun) /* Solaris requires -lsocket -lnsl for inet_pton() */ From 369d7c71485c9a63ab616d7bd69cfcd391d7637c Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 16 Jun 2023 14:16:03 +0200 Subject: [PATCH 4/6] Don't use FindFirstFileW() before Windows XP On runtime it will attempt to get FindFirstFileW() from kernel32.dll but it's not there and the DLL/program containing mbedtls will not load. Signed-off-by: Steve Lhomme --- library/x509_crt.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/x509_crt.c b/library/x509_crt.c index 453389b7ef..a784e634dd 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -1540,6 +1540,7 @@ int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path) { int ret = 0; #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) +#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_XP */ int w_ret; WCHAR szDir[MAX_PATH]; char filename[MAX_PATH]; @@ -1602,6 +1603,9 @@ int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path) cleanup: FindClose(hFind); +#else /* !_WIN32_WINNT_XP */ +#error mbedtls_x509_crt_parse_path not available before Windows XP +#endif /* !_WIN32_WINNT_XP */ #else /* _WIN32 */ int t_ret; int snp_ret; From 4000b6ec0ef0aa6165bbf3b1f015f9cb21529472 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 16 Jun 2023 14:17:21 +0200 Subject: [PATCH 5/6] Don't force the default windows version down The _WIN32_WINNT value will pick the default value for the SDK when including windows.h. Depending on its value some calls will be possible or not. Signed-off-by: Steve Lhomme --- library/x509_crt.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/x509_crt.c b/library/x509_crt.c index a784e634dd..e3b30db988 100644 --- a/library/x509_crt.c +++ b/library/x509_crt.c @@ -61,9 +61,6 @@ #if defined(MBEDTLS_HAVE_TIME) #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) #define WIN32_LEAN_AND_MEAN -#ifndef _WIN32_WINNT -#define _WIN32_WINNT 0x0600 -#endif #include #else #include From 551b3bf4bbde8160a97b32a0a760d617fedf9a70 Mon Sep 17 00:00:00 2001 From: Steve Lhomme Date: Fri, 16 Jun 2023 14:26:51 +0200 Subject: [PATCH 6/6] Don't force the _WIN32_WINT version If the user has set a value in the build environment, it will be used. Otherwise, if SDK has a default value, it will be picked. If either of these values are lower than 0x0501 (XP) we should not force some calls that will not work on the minimum target OS. We should use dynamic loading of these API's to support them in higher versions of the OS. winsock2.h needs to be included before windows.h and will pick the default _WIN32_WINNT from the SDK or use the one from the user, by setting _WIN32_WINNT in the CFLAGS. Signed-off-by: Steve Lhomme --- library/net_sockets.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/library/net_sockets.c b/library/net_sockets.c index e63d08b15d..dcc85ae7bc 100644 --- a/library/net_sockets.c +++ b/library/net_sockets.c @@ -49,11 +49,6 @@ #define IS_EINTR(ret) ((ret) == WSAEINTR) -#if !defined(_WIN32_WINNT) -/* Enables getaddrinfo() & Co */ -#define _WIN32_WINNT 0x0501 -#endif - #include #include