From 720c13fa4884d8bd691a2c32795b9bc2230ed4e7 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 22 Feb 2021 15:35:10 +0100 Subject: Make libpassphrase optional MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- DEPENDENCIES | 4 ++-- config.mk | 4 +++- file2key.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/DEPENDENCIES b/DEPENDENCIES index af6600c..ee2bbd7 100644 --- a/DEPENDENCIES +++ b/DEPENDENCIES @@ -1,7 +1,7 @@ RUNTIME DEPENDENCIES: libc - libpassphrase + libpassphrase (opt-in) libkeccak @@ -11,7 +11,7 @@ BUILD DEPENDENCIES: make cc libc - libpassphrase + libpassphrase (opt-in) libkeccak diff --git a/config.mk b/config.mk index 42aeeb4..b8c111e 100644 --- a/config.mk +++ b/config.mk @@ -3,4 +3,6 @@ MANPREFIX = $(CONFIGFILE) CFLAGS = -std=c99 -Wall -Wextra -O2 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -LDFLAGS = -s -lpassphrase -lkeccak +LDFLAGS = -s -lkeccak + +# To use libpassphrase, add -DWITH_LIBPASSPHRASE to CPPFLAGS and -lpassphrase to LDFLAGS diff --git a/file2key.c b/file2key.c index 77b9fc3..8781330 100644 --- a/file2key.c +++ b/file2key.c @@ -4,9 +4,14 @@ #include #include #include +#ifndef WITH_LIBPASSPHRASE +# include +#endif #include -#include +#ifdef WITH_LIBPASSPHRASE +# include +#endif #include #include "config.h" @@ -53,12 +58,20 @@ static char *argv0; static int get_passphrase(char **passphrasep) { +#ifndef WITH_LIBPASSPHRASE + struct termios stty, stty_saved; + char *passphrase = NULL, *new; + size_t len = 0, size = 0; + ssize_t r; +#endif int ttyfd; ttyfd = open("/dev/tty", O_RDONLY); if (ttyfd < 0) { perror(argv0); return 2; } + +#ifdef WITH_LIBPASSPHRASE passphrase_disable_echo1(ttyfd); fprintf(stderr, "%s", PASSPHRASE_PROMPT_STRING); fflush(stderr); @@ -68,6 +81,55 @@ get_passphrase(char **passphrasep) passphrase_reenable_echo1(ttyfd); close(ttyfd); return *passphrasep ? 0 : 2; + +#else + memset(&stty, 0, sizeof(stty)); + if (tcgetattr(ttyfd, &stty)) { + perror(argv0); + close(ttyfd); + return 2; + } + memcpy(&stty_saved, &stty, sizeof(stty)); + stty.c_lflag &= (tcflag_t)~ECHO; + tcsetattr(ttyfd, TCSAFLUSH, &stty); + fprintf(stderr, "%s", PASSPHRASE_PROMPT_STRING); + fflush(stderr); + + for (;;) { + if (len == size) { + new = realloc(passphrase, size += 32); + if (!new) { + perror(argv0); + close(ttyfd); + if (passphrase) { + memset(passphrase, 0, len); + free(passphrase); + } + return 2; + } + passphrase = new; + } + r = read(ttyfd, &passphrase[len], 1); + if (r < 0) { + perror(argv0); + memset(passphrase, 0, len); + free(passphrase); + close(ttyfd); + return 2; + } else if (!r || passphrase[len] == '\n') { + passphrase[len] = 0; + break; + } else { + len += 1; + } + } + + fprintf(stderr, "\n"); + tcsetattr(ttyfd, TCSAFLUSH, &stty_saved); + close(ttyfd); + *passphrasep = passphrase; + return 0; +#endif } @@ -188,7 +250,11 @@ main(int argc, char *argv[]) goto fail; if (libkeccak_update(&state, passphrase, strlen(passphrase))) goto pfail; +#ifdef WITH_LIBPASSPHRASE passphrase_wipe(passphrase, strlen(passphrase)); +#else + memset(passphrase, 0, strlen(passphrase)); +#endif free(passphrase); passphrase = NULL; @@ -238,7 +304,11 @@ pfail: perror(*argv); fail: if (passphrase) { +#ifdef WITH_LIBPASSPHRASE passphrase_wipe(passphrase, strlen(passphrase)); +#else + memset(passphrase, 0, strlen(passphrase)); +#endif free(passphrase); } done: -- cgit v1.2.3-70-g09d2