Files
sshcrypt/tests/run.sh
T
lotherk c65e756cac
ci / build (push) Successful in 35s
read the OpenSSH key container ourselves
ssh-keygen -p -m PKCS8 is not a conversion path: on OpenSSH 10.2 (Ubuntu
26.04 LTS) it ignores -m for ed25519 keys, exits 0, and leaves the container,
so every ed25519 decryption failed there. RSA still converted, which hid it.

src/openssh.c now parses the unencrypted openssh-key-v1 container directly:
ed25519 seed plus public key, or RSA n/e/d/iqmp/p/q, with the public and
private halves checked against each other. ssh-keygen is left with one job,
stripping a passphrase (-p -N ''), which works on every version. Unencrypted
keys no longer need the program at all, which the suite now asserts by
running one decryption with PATH=/nonexistent.

Verified on Debian bookworm (OpenSSH 9.2) and Ubuntu 26.04 (10.2).
2026-11-21 17:11:26 +00:00

181 lines
6.6 KiB
Bash
Executable File

#!/bin/sh
# End-to-end tests. No network, no fixtures: keys are made in a temp dir.
set -eu
cd "$(dirname "$0")/.."
BIN=$PWD/sshcrypt
T=$(mktemp -d "${TMPDIR:-/tmp}/sshcrypt-test.XXXXXX")
trap 'rm -rf "$T"' EXIT INT TERM
pass=0
fail=0
ok() { pass=$((pass + 1)); }
bad() { fail=$((fail + 1)); printf 'FAIL: %s\n' "$*" >&2; }
check() { if eval "$2"; then ok; else bad "$1"; fi; }
ssh-keygen -q -t ed25519 -N '' -C t -f "$T/ed" >/dev/null
ssh-keygen -q -t rsa -b 3072 -N '' -C t -f "$T/rsa" >/dev/null
ssh-keygen -q -t ed25519 -N '' -C other -f "$T/other" >/dev/null
ssh-keygen -q -t ed25519 -N 'hunter2' -C pw -f "$T/pw" >/dev/null
# --- fingerprints agree with OpenSSH -----------------------------------------
check "fingerprint ed25519 matches ssh-keygen" \
'[ "$("$BIN" -F "$T/ed.pub")" = "$(ssh-keygen -lf "$T/ed.pub" | cut -d" " -f2)" ]'
check "fingerprint RSA matches ssh-keygen" \
'[ "$("$BIN" -F "$T/rsa.pub")" = "$(ssh-keygen -lf "$T/rsa.pub" | cut -d" " -f2)" ]'
# --- round trips --------------------------------------------------------------
printf 'hello, world\n' >"$T/msg"
"$BIN" -r "$T/ed.pub" -i "$T/msg" -o "$T/msg.asc"
check "armored output is text" 'grep -q "BEGIN SSHCRYPT MESSAGE" "$T/msg.asc"'
"$BIN" -d -k "$T/ed" -i "$T/msg.asc" -o "$T/msg.out"
check "ed25519 round trip" 'cmp -s "$T/msg" "$T/msg.out"'
"$BIN" -d -k "$T/ed" -i "$T/msg.asc" -o "$T/msg.same" -q
check "round trip is byte exact" 'cmp -s "$T/msg" "$T/msg.same"'
head -c 65536 /dev/urandom >"$T/blob"
"$BIN" -r "$T/ed.pub" -b -i "$T/blob" -o "$T/blob.bin"
check "binary envelope has the magic" 'head -c 4 "$T/blob.bin" | grep -q SSHC'
"$BIN" -d -k "$T/ed" -i "$T/blob.bin" -o "$T/blob.out"
check "64 KiB random binary round trip" 'cmp -s "$T/blob" "$T/blob.out"'
: >"$T/empty"
"$BIN" -r "$T/ed.pub" -i "$T/empty" -o "$T/empty.asc"
"$BIN" -d -k "$T/ed" -i "$T/empty.asc" -o "$T/empty.out"
check "empty input round trip" '[ ! -s "$T/empty.out" ]'
printf 'no trailing newline\000and a nul' >"$T/odd"
"$BIN" -r "$T/ed.pub" -i "$T/odd" -o "$T/odd.asc"
"$BIN" -d -k "$T/ed" -i "$T/odd.asc" -o "$T/odd.out"
check "NUL bytes and no trailing newline" 'cmp -s "$T/odd" "$T/odd.out"'
# --- pipes -------------------------------------------------------------------
cat "$T/msg" | "$BIN" -r "$T/ed.pub" | "$BIN" -d -k "$T/ed" >"$T/pipe.out"
check "stdin to stdout pipeline" 'cmp -s "$T/msg" "$T/pipe.out"'
# --- RSA ---------------------------------------------------------------------
"$BIN" -r "$T/rsa.pub" -i "$T/msg" -o "$T/rsa.asc"
"$BIN" -d -k "$T/rsa" -i "$T/rsa.asc" -o "$T/rsa.out"
check "RSA round trip" 'cmp -s "$T/msg" "$T/rsa.out"'
# --- armor survives being pasted into a mail ---------------------------------
{
printf 'Hi, here is the thing:\n\n'
cat "$T/msg.asc"
printf '\n-- \nsig\n'
} >"$T/mail.txt"
"$BIN" -d -k "$T/ed" -i "$T/mail.txt" -o "$T/mail.out"
check "armor inside surrounding text" 'cmp -s "$T/msg" "$T/mail.out"'
# --- errors ------------------------------------------------------------------
if "$BIN" -d -k "$T/other" -i "$T/msg.asc" -o "$T/nope" 2>"$T/err"; then
bad "decrypting with the wrong key must fail"
else
ok
fi
check "wrong key names both fingerprints" \
'grep -q "but your key is SHA256:" "$T/err"'
check "wrong key writes nothing" '[ ! -e "$T/nope" ]'
cp "$T/blob.bin" "$T/tampered.bin"
python3 - "$T/tampered.bin" <<'EOF'
import sys
p = sys.argv[1]
b = bytearray(open(p, 'rb').read())
b[-1] ^= 0x01
open(p, 'wb').write(b)
EOF
if "$BIN" -d -k "$T/ed" -i "$T/tampered.bin" -o "$T/nope2" 2>"$T/err2"; then
bad "a modified message must fail"
else
ok
fi
if "$BIN" -r "$T/ed" -d -k "$T/ed" -i "$T/msg" -o "$T/nope3" 2>/dev/null; then
bad "garbage input must fail"
else
ok
fi
# --- passphrase-protected OpenSSH key, and -r with a private key --------------
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 -d "\r" | tr " " "\n" |
grep -qx echo'
# Interrupting the conversion must not leave key material behind.
if command -v pgrep >/dev/null 2>&1; then
tmpd=/dev/shm
[ -w "$tmpd" ] || tmpd=${TMPDIR:-/tmp}
before=$(ls "$tmpd" 2>/dev/null | grep -c '^sshcrypt-' || true)
mkfifo "$T/ttyin"
exec 9<>"$T/ttyin"
script -qec \
"\"$BIN\" -d -k \"$T/pw\" -i \"$T/pw.asc\" -o \"$T/sig.out\"" \
/dev/null <&9 >"$T/sig.log" 2>&1 &
ptyjob=$!
ready=0
i=0
while [ "$i" -lt 200 ]; do
grep -q "passphrase for" "$T/sig.log" 2>/dev/null &&
{ ready=1; break; }
sleep 0.1
i=$((i + 1))
done
if [ "$ready" -eq 1 ]; then
pid=$(pgrep -x sshcrypt | head -1)
[ -n "$pid" ] && kill -INT "$pid" 2>/dev/null
wait "$ptyjob" 2>/dev/null || true
exec 9>&-
after=$(ls "$tmpd" 2>/dev/null |
grep -c '^sshcrypt-' || true)
check "SIGINT at the prompt leaves no temp key" \
'[ "$after" -eq "$before" ]'
else
printf 'skip: the tty prompt did not come up\n'
kill "$ptyjob" 2>/dev/null || true
exec 9>&-
fi
fi
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"'
# An unencrypted OpenSSH key is parsed here, not by ssh-keygen.
env PATH=/nonexistent "$BIN" -d -k "$T/ed" -i "$T/msg.asc" \
-o "$T/nossh.out" >/dev/null 2>&1
check "unencrypted keys need no ssh-keygen" 'cmp -s "$T/msg" "$T/nossh.out"'
ssh-keygen -q -t rsa -b 3072 -N 'hunter2' -C pwr -f "$T/pwr" >/dev/null
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -r "$T/pwr.pub" -i "$T/msg" -o "$T/pwr.asc"
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -d -k "$T/pwr" -i "$T/pwr.asc" \
-o "$T/pwr.out"
check "passphrase-protected RSA key" 'cmp -s "$T/msg" "$T/pwr.out"'
# --- 1 MiB -------------------------------------------------------------------
head -c 1048576 /dev/urandom >"$T/big"
"$BIN" -r "$T/ed.pub" -i "$T/big" -o "$T/big.asc"
"$BIN" -d -k "$T/ed" -i "$T/big.asc" -o "$T/big.out"
check "1 MiB round trip" 'cmp -s "$T/big" "$T/big.out"'
printf '%d passed, %d failed\n' "$pass" "$fail"
[ "$fail" -eq 0 ]