diff --git a/AGENTS.md b/AGENTS.md index 53155d7..5846b45 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,7 +11,7 @@ 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: 31 end-to-end cases. It needs `ssh-keygen`, `cmp`, +`make check` is the gate: 35 end-to-end cases. It needs `ssh-keygen`, `cmp`, `python3` (tamper and crafted-key cases), `script(1)` (pty cases), `pgrep` (SIGINT case) and `setsid(1)` (no-tty case); those skip themselves if the tool is missing. No network. After touching parsing or the envelope, also run @@ -77,6 +77,12 @@ you pass; do not remove that `override`. a field that merely contains a type name is skipped unless its base64 decodes to that same type. A substring search accepts `# disabled: ssh-ed25519 ...` and breaks on `command="echo ssh-rsa hi"`. +* `SCR_INPUT_MAX` bounds the *input*, not the output. It used to guard the + envelope size too, so an input of exactly the documented cap was rejected by + the reader and again by `scr_encrypt`. The reader now probes one byte past a + full buffer to distinguish "full" from "too large", and `scr_encrypt` only + guards against `size_t` overflow. `tests/run.sh` rebuilds the tool with a + 64 KiB cap so both sides of the boundary are covered without a 1 GiB file. * `scr_die()` is declared `SCR_NORETURN`. Without the attribute both `gcc -fanalyzer` and `clang --analyze` treat the code after a fatal call as reachable and report a NULL dereference and a double free that cannot happen. diff --git a/README.md b/README.md index d1e7b11..6cc3ab8 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ Code: https://git.kropa.tech/hiddenbox/sshcrypt `-r` also accepts a private key path: the public half is derived from it, so `sshcrypt -r ~/.ssh/id_ed25519` works when you want to encrypt to yourself. +A passphrase-protected key asks for its passphrase first, because the public +half is read out of the key. Nothing is written to stdout except the payload. The one-line `encrypted for SHA256:...` note goes to stderr, and only when stderr is a diff --git a/docs/sshcrypt.1 b/docs/sshcrypt.1 index 64fce01..6ee710a 100644 --- a/docs/sshcrypt.1 +++ b/docs/sshcrypt.1 @@ -63,6 +63,7 @@ The recipient's public key, in .I authorized_keys format. A private key is accepted too: its public half is derived from it. +If that key has a passphrase, it is asked for first. A file that holds more than one supported key is rejected. .TP .BR \-k ", " \-\-key " " \fIFILE\fP diff --git a/src/envelope.c b/src/envelope.c index 6671d5b..c434f3f 100644 --- a/src/envelope.c +++ b/src/envelope.c @@ -301,7 +301,9 @@ int scr_encrypt(const struct scr_pub *to, const unsigned char *pt, size_t n, goto done; body = SCR_HDR_LEN + 2 + kemlen + SCR_NONCE_LEN; - if (n > SCR_INPUT_MAX - body) + /* Overflow guard only: the reader already enforced the input cap, and + * refusing exactly-cap input here would contradict it. */ + if (n > (size_t)-1 - body - SCR_TAG_LEN) goto done; env = scr_xmalloc(body + n + SCR_TAG_LEN); memcpy(env, header, SCR_HDR_LEN); diff --git a/src/sshcrypt.h b/src/sshcrypt.h index 97b6277..4f331a3 100644 --- a/src/sshcrypt.h +++ b/src/sshcrypt.h @@ -23,7 +23,9 @@ #define SCR_TAG_LEN 16 #define SCR_HDR_LEN 40 /* magic4 version1 suite1 reserved2 fingerprint32 */ #define SCR_PUB_MAX 4096 +#ifndef SCR_INPUT_MAX #define SCR_INPUT_MAX (1024u * 1024u * 1024u) +#endif #define SCR_OK 0 #define SCR_EUSAGE 1 diff --git a/src/util.c b/src/util.c index c88bbbb..726056d 100644 --- a/src/util.c +++ b/src/util.c @@ -66,10 +66,23 @@ struct buf scr_read_fd(int fd, size_t max, const char *what) ssize_t r; if (b.len == cap) { - if (cap >= max) + if (cap >= max) { + /* A fill of exactly max bytes is allowed; only one + * more byte proves the input is too large. */ + unsigned char extra; + + do { + r = read(fd, &extra, 1); + } while (r < 0 && errno == EINTR); + if (r < 0) + scr_die(SCR_EIO, "%s: %s", what, + strerror(errno)); + if (r == 0) + break; scr_die(SCR_EDATA, "%s: larger than the %zu byte limit", what, max); + } cap *= 2; if (cap > max) cap = max; diff --git a/tests/run.sh b/tests/run.sh index 5a7be02..768d0f8 100755 --- a/tests/run.sh +++ b/tests/run.sh @@ -92,6 +92,15 @@ check "RSA round trip" 'cmp -s "$T/msg" "$T/rsa.out"' "$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" @@ -102,6 +111,12 @@ 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 @@ -248,5 +263,28 @@ head -c 1048576 /dev/urandom >"$T/big" "$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 ]