From ac5cabaac9b84d7eda468b9dacbc760306f10cb6 Mon Sep 17 00:00:00 2001 From: Daniel Pouzzner Date: Thu, 6 Jun 2024 17:14:12 -0500 Subject: [PATCH] fixes for USE_WINDOWS_API && !NO_FILESYSTEM && !NO_WOLFSSL_DIR: * in wc_port.h, add XWRITE and XREAD definitions and include ; * in wolfSSL_BIO_read(), implement Windows support for XREAD and XWRITE; * in wolfSSL_BIO_write_filename(), add 'b' flag to XFOPEN flags; * in wolfSSL_RAND_file_name(), add support for XALTHOMEVARNAME, and add Windows definition for it to wc_port.h alongside XWRITE and XREAD. fixes test_wolfSSL_BIO, test_wolfSSL_X509_print, test_wolfSSL_RAND, test_wolfSSL_RSA_print in cross-mingw-all-crypto scenario. --- src/bio.c | 11 ++++++++--- src/ssl.c | 11 ++++++++++- wolfssl/wolfcrypt/wc_port.h | 4 ++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/bio.c b/src/bio.c index 1d78b3aa54..e6d8d83c96 100644 --- a/src/bio.c +++ b/src/bio.c @@ -312,7 +312,7 @@ int wolfSSL_BIO_read(WOLFSSL_BIO* bio, void* buf, int len) ret = (int)XFREAD(buf, 1, (size_t)len, (XFILE)bio->ptr); } else { - #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR) && \ + #if defined(XREAD) && !defined(NO_WOLFSSL_DIR) && \ !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) ret = (int)XREAD(bio->num, buf, (size_t)len); #else @@ -682,7 +682,7 @@ int wolfSSL_BIO_write(WOLFSSL_BIO* bio, const void* data, int len) ret = (int)XFWRITE(data, 1, (size_t)len, (XFILE)bio->ptr); } else { - #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR) && \ + #if defined(XWRITE) && !defined(NO_WOLFSSL_DIR) && \ !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) ret = (int)XWRITE(bio->num, data, (size_t)len); #else @@ -1617,7 +1617,12 @@ int wolfSSL_BIO_write_filename(WOLFSSL_BIO *bio, char *name) XFCLOSE((XFILE)bio->ptr); } - bio->ptr = XFOPEN(name, "w"); + /* 'b' flag is ignored on POSIX targets, but on Windows it assures + * inhibition of LF<->CRLF rewriting, so that there is consistency + * between the size and contents of the representation in memory and on + * disk. + */ + bio->ptr = XFOPEN(name, "wb"); if (((XFILE)bio->ptr) == XBADFILE) { return WOLFSSL_FAILURE; } diff --git a/src/ssl.c b/src/ssl.c index becadffd07..51732a5086 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -23701,9 +23701,18 @@ const char* wolfSSL_RAND_file_name(char* fname, unsigned long len) const char ap[] = "/.rnd"; WOLFSSL_MSG("Environment variable RANDFILE not set"); + if ((rt = XGETENV("HOME")) == NULL) { + #ifdef XALTHOMEVARNAME + if ((rt = XGETENV(XALTHOMEVARNAME)) == NULL) { + WOLFSSL_MSG("Environment variable HOME and " XALTHOMEVARNAME + " not set"); + return NULL; + } + #else WOLFSSL_MSG("Environment variable HOME not set"); return NULL; + #endif } if (len > XSTRLEN(rt) + XSTRLEN(ap)) { @@ -23713,7 +23722,7 @@ const char* wolfSSL_RAND_file_name(char* fname, unsigned long len) return fname; } else { - WOLFSSL_MSG("HOME too large for buffer"); + WOLFSSL_MSG("Path too large for buffer"); return NULL; } } diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 878e31913f..3414cd6373 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -716,12 +716,16 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void); #if !defined(NO_WOLFSSL_DIR)\ && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) #if defined(USE_WINDOWS_API) + #include #include #ifndef XSTAT #define XSTAT _stat #endif #define XS_ISREG(s) (s & _S_IFREG) #define SEPARATOR_CHAR ';' + #define XWRITE _write + #define XREAD _read + #define XALTHOMEVARNAME "USERPROFILE" #elif defined(ARDUINO) #ifndef XSTAT