Use separate macros for fopen("rb") and fopen("wr") operations + don't use _SH_SECURE, because it apparently doesn't work on Windows XP.

This commit is contained in:
LoRd_MuldeR 2021-06-27 15:12:09 +02:00
parent 198e8cd947
commit ac67ca9b59
6 changed files with 48 additions and 42 deletions

View File

@ -41,14 +41,14 @@ int MAIN(int argc, CHAR_T *argv[])
/* Open output files */ /* Open output files */
/*-------------------------------------------------------*/ /*-------------------------------------------------------*/
file_pubkey = FOPEN(argv[2], T("wb")); file_pubkey = FOPEN_WR(argv[2]);
if (!file_pubkey) if (!file_pubkey)
{ {
fputs("Error: Failed to open output file for public key!\n\n", stderr); fputs("Error: Failed to open output file for public key!\n\n", stderr);
goto clean_up; goto clean_up;
} }
file_privkey = FOPEN(argv[3], T("wb")); file_privkey = FOPEN_WR(argv[3]);
if (!file_privkey) if (!file_privkey)
{ {
fputs("Error: Failed to open output file for private key!\n\n", stderr); fputs("Error: Failed to open output file for private key!\n\n", stderr);

View File

@ -43,21 +43,21 @@ int MAIN(int argc, CHAR_T *argv[])
/* Open input/output files */ /* Open input/output files */
/*-------------------------------------------------------*/ /*-------------------------------------------------------*/
file_privkey = FOPEN(argv[2], T("rb")); file_privkey = FOPEN_RD(argv[2]);
if (!file_privkey) if (!file_privkey)
{ {
fputs("Error: Failed to open private key file!\n\n", stderr); fputs("Error: Failed to open private key file!\n\n", stderr);
goto clean_up; goto clean_up;
} }
file_data = FOPEN(argv[3], T("rb")); file_data = FOPEN_RD(argv[3]);
if (!file_data) if (!file_data)
{ {
fputs("Error: Failed to open input file to be signed!\n\n", stderr); fputs("Error: Failed to open input file to be signed!\n\n", stderr);
goto clean_up; goto clean_up;
} }
file_signature = FOPEN(argv[4], T("wb")); file_signature = FOPEN_WR(argv[4]);
if (!file_signature) if (!file_signature)
{ {
fputs("Error: Failed to open output file for signature!\n\n", stderr); fputs("Error: Failed to open output file for signature!\n\n", stderr);

View File

@ -72,14 +72,14 @@ int MAIN(int argc, CHAR_T *argv[])
#ifdef EMBED_PUBKEY #ifdef EMBED_PUBKEY
public_key = load_resource_data(T("RSA_PUBLIC_KEY"), &pubkey_length); public_key = load_resource_data(L"RSA_PUBLIC_KEY", &pubkey_length);
if (!public_key) if (!public_key)
{ {
fputs("Error: Failed to load public key data from resources!\n\n", stderr); fputs("Error: Failed to load public key data from resources!\n\n", stderr);
goto clean_up; goto clean_up;
} }
checksum_pubkey = load_resource_data(T("CHECKSUM_SHA512"), &checksum_length); checksum_pubkey = load_resource_data(L"CHECKSUM_SHA512", &checksum_length);
if ((!checksum_pubkey) || (checksum_length < SHA512_DIGEST_LENGTH)) if ((!checksum_pubkey) || (checksum_length < SHA512_DIGEST_LENGTH))
{ {
fputs("Error: Failed to load public key checksum from resources!\n\n", stderr); fputs("Error: Failed to load public key checksum from resources!\n\n", stderr);
@ -113,7 +113,7 @@ int MAIN(int argc, CHAR_T *argv[])
#else #else
file_pubkey = FOPEN(argv[1], T("rb")); file_pubkey = FOPEN_RD(argv[1]);
if (!file_pubkey) if (!file_pubkey)
{ {
fputs("Error: Failed to open public key input file!\n\n", stderr); fputs("Error: Failed to open public key input file!\n\n", stderr);
@ -138,14 +138,14 @@ int MAIN(int argc, CHAR_T *argv[])
/* Open input files */ /* Open input files */
/*-------------------------------------------------------*/ /*-------------------------------------------------------*/
file_data = FOPEN(ARGV_INPUTFILE, T("rb")); file_data = FOPEN_RD(ARGV_INPUTFILE);
if (!file_data) if (!file_data)
{ {
fputs("Error: Failed to open the input file!\n\n", stderr); fputs("Error: Failed to open the input file!\n\n", stderr);
goto clean_up; goto clean_up;
} }
file_signature = FOPEN(ARGV_SIGNATURE, T("rb")); file_signature = FOPEN_RD(ARGV_SIGNATURE);
if (!file_signature) if (!file_signature)
{ {
fputs("Error: Failed to open the signature file!\n\n", stderr); fputs("Error: Failed to open the signature file!\n\n", stderr);
@ -183,15 +183,20 @@ int MAIN(int argc, CHAR_T *argv[])
goto clean_up; goto clean_up;
} }
if (source_length <= 2U + sizeof(uint64_t)) if (source_length <= sizeof(uint64_t))
{ {
fputs("Error: Signature binary data appears to be truncated!\n\n", stderr); fputs("Error: Signature binary data appears to be truncated!\n\n", stderr);
goto clean_up; goto clean_up;
} }
if (base64[base64_length - 1U] == '=') while ((base64_length > 0U) && (source_length > sizeof(uint64_t) + 1U))
{ {
source_length -= (base64[base64_length - 2U] == '=') ? 2U : 1U; /*remove padding!*/ if (base64[base64_length - 1U] != '=')
{
break;
}
--source_length;
--base64_length;
} }
/*-------------------------------------------------------*/ /*-------------------------------------------------------*/

View File

@ -152,11 +152,6 @@ static uint64_t get_system_time(void)
uint64_t get_current_time_usec(void) uint64_t get_current_time_usec(void)
{ {
/* struct timeval t;
if(gettimeofday(&t, NULL) == 0)
{
return (((uint64_t)t.tv_sec) * 1000000ULL) + ((uint64_t)t.tv_usec);
} */
const uint64_t system_time = get_system_time(); const uint64_t system_time = get_system_time();
if (system_time >= FILETIME_1970) if (system_time >= FILETIME_1970)
{ {
@ -177,7 +172,7 @@ uint64_t get_current_time_usec(void)
return 0ULL; return 0ULL;
} }
#endif #endif /*_WIN32*/
/*-------------------------------------------------------*/ /*-------------------------------------------------------*/
/* store_uint64() / load_uint64() */ /* store_uint64() / load_uint64() */
@ -208,13 +203,29 @@ uint64_t load_uint64(const unsigned char *const buffer)
(((uint64_t)buffer[7U])); (((uint64_t)buffer[7U]));
} }
/*-------------------------------------------------------*/
/* force_flush() */
/*-------------------------------------------------------*/
int force_flush(FILE *const file)
{
const int result = fflush(file);
#ifdef _WIN32
FlushFileBuffers((HANDLE)_get_osfhandle(_fileno(file)));
#else
fsync(fileno(file));
#endif
return (result != EOF);
}
/*-------------------------------------------------------*/ /*-------------------------------------------------------*/
/* load_resource_data() */ /* load_resource_data() */
/*-------------------------------------------------------*/ /*-------------------------------------------------------*/
const unsigned char* load_resource_data(const CHAR_T *const name, unsigned int *const length)
{
#ifdef _WIN32 #ifdef _WIN32
const unsigned char *load_resource_data(const wchar_t *const name, unsigned int *const length)
{
HRSRC resource = FindResourceW(NULL, name, RT_RCDATA); HRSRC resource = FindResourceW(NULL, name, RT_RCDATA);
if (resource) if (resource)
{ {
@ -228,22 +239,8 @@ const unsigned char* load_resource_data(const CHAR_T *const name, unsigned int *
} }
} }
} }
#endif
*length = 0U; *length = 0U;
return NULL; return NULL;
} }
/*-------------------------------------------------------*/ #endif /*_WIN32*/
/* force_flush() */
/*-------------------------------------------------------*/
int force_flush(FILE *const file)
{
const int result = fflush(file);
#ifdef _WIN32
FlushFileBuffers((HANDLE)_get_osfhandle(_fileno(file)));
#else
fsync(fileno(file));
#endif
return (result == 0);
}

View File

@ -20,7 +20,12 @@ char *read_line_from_file(FILE *const file, const int trim);
uint64_t get_current_time_usec(void); uint64_t get_current_time_usec(void);
void store_uint64(unsigned char *const buffer, const uint64_t value); void store_uint64(unsigned char *const buffer, const uint64_t value);
uint64_t load_uint64(const unsigned char *const buffer); uint64_t load_uint64(const unsigned char *const buffer);
const unsigned char* load_resource_data(const CHAR_T *const name, unsigned int *const length);
int force_flush(FILE *const file); int force_flush(FILE *const file);
#ifdef _WIN32
const unsigned char *load_resource_data(const wchar_t *const name, unsigned int *const length);
#else
#define load_resource_data(X,Y) ((const unsigned char*)NULL)
#endif
#endif /*_COMMON_H*/ #endif /*_COMMON_H*/

View File

@ -11,16 +11,15 @@
#define CHAR_T wchar_t #define CHAR_T wchar_t
#define MAIN wmain #define MAIN wmain
#define FOPEN(X,Y) _wfsopen((X),(Y),_SH_SECURE) #define FOPEN_RD(X) _wfsopen((X), L"rb", _SH_DENYWR)
#define _T(X) L##X #define FOPEN_WR(X) _wfsopen((X), L"wb", _SH_DENYRW)
#define T(X) _T(X)
#else #else
#define CHAR_T char #define CHAR_T char
#define MAIN main #define MAIN main
#define FOPEN(X,Y) fopen((X),(Y)) #define FOPEN_RD(X) fopen((X), "rb")
#define T(X) X #define FOPEN_WR(X) fopen((X), "wb")
#endif #endif