diff --git a/AGENTS.md b/AGENTS.md index 4b8a5f4..1e20654 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,8 +11,9 @@ those is a design decision, not a commit. make check # tests/run.sh, generates keys in a tmpdir, no network make install PREFIX=$HOME/.local # installs bin/sshcrypt -`make check` is the gate: 20 end-to-end cases. It needs `ssh-keygen`, `cmp` and -`python3` (one tamper test). After touching parsing or the envelope, also run +`make check` is the gate: 22 end-to-end cases. It needs `ssh-keygen`, `cmp`, +`python3` (one tamper test) and `script(1)` (two pty cases, skipped if absent). +No network. After touching parsing or the envelope, also run make clean && make CFLAGS="-O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer" LDFLAGS="-fsanitize=address,undefined" && make check @@ -50,6 +51,11 @@ you pass; do not remove that `override`. `keys.c` is the standard `u = (1+y)/(1-y)` map, cross-checked against libcrypto X25519 in `tests/run.sh`. Do not replace it with an assumption. * `memmem` is GNU, not POSIX. Use `find_bytes()` in `keys.c`. +* Terminal echo: save termios *before* clearing `ECHO` and restore that saved + copy. Re-reading with `tcgetattr` after disabling restores no-echo, and + termios outlives the fd, so the user's shell is left mute. Use `TCSANOW`, + not `TCSAFLUSH`: the flush discards a passphrase that is already queued in + the tty. Both are covered by the `script(1)` cases in `make check`. * Every `.c` includes its own headers; missing `` is a hard error under `-std=c99 -Wpedantic`. diff --git a/src/keys.c b/src/keys.c index 106a324..d11b755 100644 --- a/src/keys.c +++ b/src/keys.c @@ -33,31 +33,38 @@ static const char *passphrase_env(void) /* Read a line from /dev/tty with echo off. Never from stdin: that is data. */ static char *prompt_passphrase(const char *path) { - struct termios old, noecho; + struct termios saved, noecho; char *line = NULL; size_t cap = 0; ssize_t n; FILE *tty; + int have_termios; int fd = open("/dev/tty", O_RDWR); if (fd < 0) return NULL; - if (tcgetattr(fd, &old) == 0) { - noecho = old; + have_termios = tcgetattr(fd, &saved) == 0; + if (have_termios) { + noecho = saved; noecho.c_lflag &= ~(tcflag_t)ECHO; - tcsetattr(fd, TCSAFLUSH, &noecho); + /* TCSANOW, not TCSAFLUSH: do not throw away a passphrase that is + * already queued in the tty (piped or pre-typed input). */ + (void)tcsetattr(fd, TCSANOW, &noecho); } fprintf(stderr, "passphrase for %s: ", path); fflush(stderr); tty = fdopen(fd, "r"); if (!tty) { + if (have_termios) + (void)tcsetattr(fd, TCSANOW, &saved); close(fd); return NULL; } n = getline(&line, &cap, tty); fputc('\n', stderr); - if (tcgetattr(fd, &old) == 0) - tcsetattr(fd, TCSAFLUSH, &old); + /* restore the saved state, not a fresh read of the no-echo state */ + if (have_termios) + (void)tcsetattr(fd, TCSANOW, &saved); fclose(tty); if (n <= 0) { free(line); diff --git a/tests/run.sh b/tests/run.sh index df6755e..bb5d2e3 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -103,6 +103,22 @@ SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -r "$T/pw.pub" -i "$T/msg" -o "$T/pw.asc" SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -d -k "$T/pw" -i "$T/pw.asc" -o "$T/pw.out" check "passphrase-protected key round trip" 'cmp -s "$T/msg" "$T/pw.out"' +# --- the interactive prompt, on a real pty ------------------------------------ +if command -v script >/dev/null 2>&1; then + printf 'hunter2\n' | script -qec \ + "\"$BIN\" -d -k \"$T/pw\" -i \"$T/pw.asc\" -o \"$T/pty.out\"" \ + /dev/null >/dev/null + check "passphrase typed on the tty" 'cmp -s "$T/msg" "$T/pty.out"' + + out=$(printf 'hunter2\n' | script -qec \ + "\"$BIN\" -d -k \"$T/pw\" -i \"$T/pw.asc\" -o /dev/null; stty -a" \ + /dev/null) + check "echo is restored after the prompt" \ + 'printf "%s\n" "$out" | tr " " "\n" | grep -qx echo' +else + printf 'skip: script(1) missing, the tty prompt was not tested\n' +fi + "$BIN" -r "$T/ed" -i "$T/msg" -o "$T/viapriv.asc" "$BIN" -d -k "$T/ed" -i "$T/viapriv.asc" -o "$T/viapriv.out" check "encrypting to a private-key path works" 'cmp -s "$T/msg" "$T/viapriv.out"'