#!/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)" ]' # A key line inside a comment is not a key: encrypting to a disabled key would # be silent and wrong. sed 's/^/# disabled: /' "$T/ed.pub" >"$T/comment.pub" rc=0 "$BIN" -F "$T/comment.pub" >/dev/null 2>&1 || rc=$? check "a commented-out key is ignored" '[ "$rc" -ne 0 ]' # authorized_keys options may contain spaces and words that look like a key # type; only a field that decodes to that type counts. printf 'command="echo ssh-rsa hi" %s\n' "$(cat "$T/ed.pub")" >"$T/opts.pub" check "options before the key are ignored" \ '[ "$("$BIN" -F "$T/opts.pub")" = "$(ssh-keygen -lf "$T/ed.pub" | cut -d" " -f2)" ]' # Exactly one read-buffer boundary (64 KiB) ending in a newline: parse_pub_text # used to look at text[n], which is uninitialised there. python3 - "$T/ed.pub" "$T/big.pub" <<'EOF' import sys key = open(sys.argv[1]).read().strip() pad = 65536 - len(key) - 2 open(sys.argv[2], 'w').write('#' + 'x' * (pad - 1) + '\n' + key + '\n') EOF check "a 64 KiB key file is read to its last byte" \ '[ "$("$BIN" -F "$T/big.pub")" = "$(ssh-keygen -lf "$T/ed.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"' # Mail clients may have written CRLF line endings. python3 - "$T/msg.asc" "$T/crlf.asc" <<'EOF' import sys open(sys.argv[2], 'wb').write( open(sys.argv[1], 'rb').read().replace(b'\n', b'\r\n')) EOF "$BIN" -d -k "$T/ed" -i "$T/crlf.asc" -o "$T/crlf.out" check "CRLF armor decrypts" 'cmp -s "$T/msg" "$T/crlf.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" ]' # An existing output file must survive a failed decryption untouched. printf 'keep me\n' >"$T/keep" "$BIN" -d -k "$T/other" -i "$T/msg.asc" -o "$T/keep" 2>/dev/null || true check "a failed decrypt leaves the output file alone" \ '[ "$(cat "$T/keep")" = "keep me" ]' 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 ------------------------------------ tmpd=/dev/shm [ -w "$tmpd" ] || tmpd=${TMPDIR:-/tmp} # No tty and no passphrase: fail before a temp file is created. if command -v setsid >/dev/null 2>&1; then before=$(ls "$tmpd" 2>/dev/null | grep -c '^sshcrypt-' || true) rc=0 env -u SSHCRYPT_PASSPHRASE setsid -w "$BIN" -d -k "$T/pw" \ -i "$T/pw.asc" -o /dev/null /dev/null 2>&1 || rc=$? after=$(ls "$tmpd" 2>/dev/null | grep -c '^sshcrypt-' || true) check "no tty and no passphrase leaves no temp key" \ '[ "$rc" -eq 1 ] && [ "$after" -eq "$before" ]' else printf 'skip: setsid(1) missing, the no-tty case was not tested\n' fi 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 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"' # --- the input cap boundary -------------------------------------------------- # Built with a 64 KiB cap so the exact boundary is testable without a 1 GiB # file. A fill of exactly the cap must work; one byte more must not. CAP_BIN=$T/sshcrypt-cap if ${CC:-cc} -O1 -std=c99 -D_POSIX_C_SOURCE=200809L -DSCR_INPUT_MAX=65536 \ -I src -o "$CAP_BIN" src/*.c \ $(pkg-config --libs libcrypto 2>/dev/null || echo -lcrypto) 2>/dev/null then head -c 65536 /dev/urandom >"$T/exact" rc=0 "$CAP_BIN" -r "$T/ed.pub" -b -i "$T/exact" -o "$T/exact.bin" \ 2>/dev/null || rc=$? check "exactly the input cap is accepted" \ '[ "$rc" -eq 0 ] && [ -s "$T/exact.bin" ]' head -c 65537 /dev/urandom >"$T/over" rc=0 "$CAP_BIN" -r "$T/ed.pub" -b -i "$T/over" -o "$T/over.bin" \ 2>/dev/null || rc=$? check "one byte over the cap is rejected" '[ "$rc" -eq 2 ]' else printf 'skip: could not build the small-cap binary\n' fi printf '%d passed, %d failed\n' "$pass" "$fail" [ "$fail" -eq 0 ]