ci / build (push) Successful in 1m7s
Review found a stack-buffer-overflow in scr_openssh_load: the public blob length came straight from the file and was memcpy'd into struct scr_priv's 4096-byte field. A crafted RSA key file (the ed25519 path cross-checks first) overwrote the stack: ASan reported a 6000-byte write. Also reject p or q <= 1 before BN_mod, arm the temp-file cleanup before the write rather than after, clear the active flag on the conversion-failure path, and block INT/TERM/HUP across ssh-keygen so a signalled child cannot rename a fresh copy back after the handler unlinked it. The new case crafts an oversized blob and asserts a clean exit 1, not a crash.
215 lines
7.8 KiB
Bash
Executable File
215 lines
7.8 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
|
|
|
|
# A public blob larger than the 4096-byte field must be rejected, not memcpy'd
|
|
# into struct scr_priv (this was a stack overflow in the container parser).
|
|
python3 - "$T/rsa" "$T/oversize" <<'EOF'
|
|
import base64, struct, sys
|
|
def rd(b, o):
|
|
n = struct.unpack('>I', b[o:o+4])[0]
|
|
return b[o+4:o+4+n], o+4+n
|
|
def put(s):
|
|
return struct.pack('>I', len(s)) + s
|
|
body = base64.b64decode(''.join(
|
|
l for l in open(sys.argv[1]).read().splitlines() if '---' not in l))
|
|
o = 15
|
|
cipher, o = rd(body, o)
|
|
kdf, o = rd(body, o)
|
|
ko, o = rd(body, o)
|
|
o += 4
|
|
_, o = rd(body, o)
|
|
priv, o = rd(body, o)
|
|
new = (body[:15] + put(cipher) + put(kdf) + put(ko) + struct.pack('>I', 1)
|
|
+ put(b'\x00' * 6000) + put(priv))
|
|
b64 = base64.b64encode(new).decode()
|
|
open(sys.argv[2], 'w').write(
|
|
'-----BEGIN OPENSSH PRIVATE KEY-----\n'
|
|
+ '\n'.join(b64[i:i + 70] for i in range(0, len(b64), 70))
|
|
+ '\n-----END OPENSSH PRIVATE KEY-----\n')
|
|
EOF
|
|
rc=0
|
|
"$BIN" -d -k "$T/oversize" -i "$T/msg.asc" -o /dev/null 2>"$T/over.err" ||
|
|
rc=$?
|
|
check "oversized key blob is rejected, not a crash" \
|
|
'[ "$rc" -eq 1 ] || [ "$rc" -eq 2 ]'
|
|
check "oversized key blob is named as such" \
|
|
'grep -q "public key blob is" "$T/over.err"'
|
|
|
|
# --- 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 ]
|