Make it possible to read the passphrase from STDIN.

This commit is contained in:
LoRd_MuldeR 2020-10-16 19:33:12 +02:00
parent b820b75f5d
commit 1b5d8a7d4c
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
3 changed files with 25 additions and 9 deletions

View File

@ -22,24 +22,28 @@ static volatile int g_interrupted = 0;
static char* read_passphrase(const CHR* const file_name)
{
const size_t buff_size = 1024U;
char *const buffer = (char*) malloc(buff_size * sizeof(char));
static const size_t buff_size = 512U;
char *buffer = (char*) malloc(buff_size * sizeof(char));
if (!buffer)
{
return NULL;
}
FILE *const file = FOPEN(file_name, T("rb"));
const int use_stdin = (STRICMP(file_name, T("-")) == 0);
FILE *const file = use_stdin ? stdin : FOPEN(file_name, T("rb"));
if (!file)
{
free(buffer);
return NULL;
}
do
{
if (!fgets(buffer, (int)buff_size, file))
{
fclose(file);
free(buffer);
return NULL;
buffer = NULL;
goto finish;
}
size_t length = strlen(buffer);
while ((length > 0U) && ((buffer[length - 1U] == '\r') || (buffer[length - 1U] == '\n')))
@ -48,7 +52,14 @@ static char* read_passphrase(const CHR* const file_name)
}
}
while (!buffer[0U]);
fclose(file);
finish:
if ((!use_stdin) && file)
{
fclose(file);
}
return buffer;
}
@ -505,16 +516,17 @@ int MAIN(int argc, CHR* argv[])
FPUTS(T("This software has been released under the CC0 1.0 Universal license:\n"), stderr);
FPUTS(T("https://creativecommons.org/publicdomain/zero/1.0/legalcode\n"), stderr);
FPUTS(T("====================================================================\n\n"), stderr);
FPUTS(T("Usage:\n"), stderr);
FPUTS(T("Synopsis:\n"), stderr);
FPRINTF(stderr, T(" %") T(PRISTR) T(" --encrypt [[@][:]<passphrase>] <input.txt> <output.enc>\n"), program);
FPRINTF(stderr, T(" %") T(PRISTR) T(" --decrypt [[@][:]<passphrase>] <input.enc> <output.txt>\n\n"), program);
FPUTS(T("Notes:\n"), stderr);
FPUTS(T("Remarks:\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a '@' character, then it specifies the file\n"), stderr);
FPUTS(T(" to read the passphrase from; only the first line in that file is used!\n"), stderr);
FPUTS(T("- If <passphrase> is prefixed with a ':' character, then the leading character\n"), stderr);
FPUTS(T(" is skipped and the remainder of the argument is used as passphrase.\n"), stderr);
FPUTS(T("- If the argument <passphrase> is *not* present, then the environment variable\n"), stderr);
FPRINTF(stderr, T(" \"%") T(PRISTR) T("\" must be set and it specifies the passphrase to be used.\n\n"), ENVV_PASSWD_NAME);
FPRINTF(stderr, T(" \"%") T(PRISTR) T("\" must be set; it specifies the passphrase to be used.\n"), ENVV_PASSWD_NAME);
FPUTS(T("- Specify \"@-\" in order to read the passphrase from the standard input stream!\n\n"), stderr);
return 0;
}
if ((!STRICMP(argv[1U], T("-t"))) || (!STRICMP(argv[1U], T("--self-test"))))

View File

@ -41,6 +41,7 @@ void init_terminal(void)
{
#ifdef _WIN32
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
_setmode(_fileno(stdin), _O_BINARY);
_setmode(_fileno(stderr), _O_U8TEXT);
if (_acmdln) SecureZeroMemory(_acmdln, strlen(_acmdln) * sizeof(char));
if (_wcmdln) SecureZeroMemory(_wcmdln, wcslen(_wcmdln) * sizeof(wchar_t));

View File

@ -9,6 +9,9 @@
#include <stdlib.h>
#include <stdint.h>
/*
* Version info
*/
extern const char *const LIBMCRYPT_VERSION;
extern const char* const LIBMCRYPT_BUILDNO;