Articles / Security

  • age
  • ssh-keygen
  • digital-signatures
  • encryption
  • authentication
  • linux

How to Check an age File Really Came From Who You Think

A file that decrypts cleanly with your age key could have come from anyone who has your public key. You hand that key out so people can send you things. age does not record, or check, which of them did.

So if Alice sends Bob a file and Bob needs to know it was Alice, the file needs a signature as well as encryption. You probably already have the tool for the job: ssh-keygen.

Why successful decryption proves so little

With an ordinary X25519 recipient, age makes a random file key and wraps it to Bob's public key using a fresh, throwaway key pair. No long-term sender key is involved anywhere.

The encryption is still authenticated, in the AEAD sense: someone without the file key cannot flip bits without being caught. That tells you the file is intact relative to whoever made it, without telling you who that was. Someone could simply have made a whole new one.

Quick detour, because this bit is interesting. With several recipients, they all unwrap the same file key. The age format derives both the header MAC and the payload key from it, so one recipient can keep the original header, write a different payload, and the others will accept it. So recipients cannot even be sure they were all sent the same file.

The lack of signing is a deliberate scope decision. age's author, Filippo Valsorda, has written about authentication in age, including why signing was left out.

What you are going to build

Alice signs first, then encrypts the file and signature together:

  1. Wrap the file in a tar archive, so its name is covered by the signature.
  2. Sign that archive with Alice's SSH key, naming Bob as the intended recipient.
  3. Put the archive and signature into a second tar.
  4. Encrypt the result to Bob with age.

Bob decrypts, verifies, and only then unpacks the real file. You need age, GNU tar, bash and an OpenSSH recent enough to have ssh-keygen -Y.

Give Bob Alice's public key by another route

Alice should use a key kept for signing files. An Ed25519 key is a sensible choice:

ssh-keygen -t ed25519 -f alice_files
ssh-keygen -lf alice_files.pub   # prints the fingerprint

Bob needs alice_files.pub, and needs to know it is really hers. Compare the fingerprint over a channel an impostor does not control, such as in person or on a call where you know the voice. A public key that arrives alongside the file proves nothing.

Bob then writes one line into a file called allowed_signers, using the key type and base64 text from Alice's .pub file:

[email protected] namespaces="age-file:[email protected]" ssh-ed25519 AAAA...

That line says which name the key belongs to, and what it is allowed to sign for.

Alice: sign, bundle, encrypt

Save this as send.sh:

#!/usr/bin/env bash
# usage: send.sh FILE SIGNING_KEY BOB_AGE_RECIPIENT OUTPUT
set -euo pipefail
file=$(realpath "$1") key=$(realpath "$2") out=$(realpath -m "$4")
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
cd "$work"

tar -cf payload.tar -C "$(dirname "$file")" -- "$(basename "$file")"
ssh-keygen -Y sign -f "$key" -n age-file:[email protected] payload.tar
tar -cf bundle.tar payload.tar payload.tar.sig
age -r "$3" -o "$out" bundle.tar

Run it with Bob's age1... recipient string:

./send.sh report.csv alice_files age1... report.age

Bob: decrypt, verify, then unpack

Save this as receive.sh:

#!/usr/bin/env bash
# usage: receive.sh FILE.age AGE_IDENTITY ALLOWED_SIGNERS NEW_OUTPUT_DIR
set -euo pipefail
in=$(realpath "$1") id=$(realpath "$2") signers=$(realpath "$3")
out=$(realpath -m "$4")
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
cd "$work"

age -d -i "$id" -o bundle.tar "$in"
tar -xOf bundle.tar payload.tar > payload.tar
tar -xOf bundle.tar payload.tar.sig > payload.tar.sig

ssh-keygen -Y verify -f "$signers" -I [email protected] \
  -n age-file:[email protected] -s payload.tar.sig < payload.tar

mkdir "$out"
tar -xf payload.tar -C "$out"

A genuine file prints a line like this, with Alice's fingerprint at the end:

Good "age-file:[email protected]" signature for [email protected] with ED25519 key SHA256:...

Until verification passes, everything in that bundle is hostile input. The script therefore does as little with it as it can, and the order of the steps matters:

  • -O writes the two expected members to stdout, so the outer archive never chooses a path on disk.
  • set -e stops at the first failure, including a failed verification.
  • The output directory is created only after the signature checks out, and mkdir refuses to reuse an existing one.
  • age writes to a file rather than a pipe, so its exit status is checked before anything reads the result. Decryption streams, and a truncated file can produce partial output before the error.

Check that it actually says no

A verification script that has only ever seen good files is untested. I ran these with age 1.2.1 and OpenSSH 10.2:

  • Signed with the wrong key: "Could not verify signature", non-zero exit, no output directory.
  • Signed by Alice for a different namespace: "namespace does not match".
  • A plain file encrypted to Bob, with no bundle: tar fails before verification is reached.

Try the first one yourself by generating a second key and passing it to send.sh. It takes a minute, and it is the test that shows your script really does reject a bad signature.

Why the recipient is inside the signature

Suppose Alice signed only the file. Bob could decrypt it, keep her valid signature, and re-encrypt both to Carol. Carol would see something Alice really did sign and conclude Alice sent it to her. This is called surreptitious forwarding, and it can matter when the message is something like "here is the contract, signed".

The -n namespace fixes that cheaply. It is part of what gets signed, so a signature made for age-file:[email protected] fails when Carol checks for her own name. Change the address in both scripts for each recipient. For a team, you can use a group name, but members can then forward to each other undetected.

Why not encrypt first and sign the ciphertext? You could verify before decrypting, which is attractive. But anyone who intercepts it can strip the signature and add their own to content they have never read, and an SSH signature exposes the signer's public key to whoever sees it. Signing inside keeps both the signature and that key private.

What a good signature still does not tell you

It tells Bob that the holder of Alice's signing key signed those bytes for that namespace. It leaves these questions open:

  • Freshness. Last month's genuine file still verifies. Put a date or sequence number in the filename, which is inside the signed archive, and have the receiving side reject old ones.
  • A stolen key. Protect the signing key with a passphrase or a hardware token. If it leaks, Bob has to remove it from allowed_signers.
  • Safe content. Alice's compromised laptop can sign something nasty perfectly well.

What about age -p, with a passphrase only the two of you know? An outsider cannot make a file that opens with it, so you get a weak sort of authentication. But either of you could have made it, anyone who learns or guesses the passphrase can too, and it says nothing about intended recipient or date. For files where the sender matters, sign them.