Mail encryption at rest
yarilo can store message bodies encrypted on disk with age keys, for maildir, sdbox and mdbox stores, and re-encrypt mail that is already stored: to a new key, or back to plaintext.
What is encrypted, what is not
| Encrypted | Stays in clear |
|---|---|
| message bodies in maildir, sdbox and mdbox files | mail indexes (flags, UIDs, sizes, cached headers and envelopes) |
| JMAP uploads while they wait to be used | the full-text search index |
| folder names, the uid list, the mdbox map |
Indexes and search stay in clear, so FETCH of headers, SEARCH and SORT cost what they cost without encryption. A body is decrypted when a client reads it, and only the 64 KiB chunks a partial FETCH touches are decrypted.
Sizes do not change for a client: RFC822.SIZE, quota and the sizes in maildir names (S=, W=) and dbox metadata count the plaintext.
A store taken over from another IMAP server that wrote the common elliptic-curve encrypted stream (P-256 keys in PEM, AES-256-GCM) is read as it is: list its keys in reference_. New mail is always written in age.
Keys
- Public key (
public_): the age recipients new mail is encrypted to. X25519 (key age1.) or hybrid post-quantum (. . age1pq1.); one type per file.. . - Private keys (
private_): the age identities that open mail, one per line. During a rotation the file holds the old key and the new one.keys - Reference keys (
reference_): PEM private keys of a store taken over from another server.keys
To generate a pair, run yarctl with the key type; the private key goes only into the file, created with mode 0600, and the public key is printed:
yarctl backend crypt keygen --type x25519 --out mail-crypt.key
yarctl backend crypt keygen --type hybrid --out mail-crypt.keyyarctl backend crypt keys shows the keys a running backend loaded, by their public halves only: which one new mail is sealed to, a public key no private key opens, and fingerprints of the reference keys.
Turning it on
The keys live in a Kubernetes Secret mounted read-only at / in every container that opens mail. To create it from Vault with the External Secrets Operator, apply:
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
name: yarilo-mail-crypt
spec:
secretStoreRef: { kind: ClusterSecretStore, name: vault }
target: { name: yarilo-mail-crypt }
data:
- secretKey: public
remoteRef: { key: mail/yarilo/mail-crypt, property: public }
- secretKey: private
remoteRef: { key: mail/yarilo/mail-crypt, property: private }To turn it on, name the Secret and set the write mode in the Helm values:
mailCrypt:
secretName: yarilo-mail-crypt
storage:
mail_crypt:
write: ageyarctl config check reports an unreadable key before a rollout.
secretName
- Set in
- Helm values
- Default
- none
- Type
- string
- Added
- 2.4.8
An existing Secret with the keys, mounted read-only at / in every container that opens mail: key public (age recipients), key private (age identities), and any other key a PEM reference key listed in reference_. Empty mounts nothing.
write
- Default
off- Type
- string
- Values
offage- Helm
storage.mail_crypt.write- See also
public_keyprivate_keys- Added
- 2.4.8
How new message bodies are stored: off as given, age encrypted to the recipients in public_. A key file that cannot be read stops the process at start; it never falls back to plaintext. Mail already on disk keeps its form until it is re-encrypted.
public_key
- Default
/etc/yarilo/mail-crypt/public- Type
- string
- Helm
storage.mail_crypt.public_key- Added
- 2.4.8
File of age recipients new mail is sealed to, read only with write: age: X25519 or hybrid post-quantum, one type per file.
private_keys
- Default
/etc/yarilo/mail-crypt/private- Type
- string
- Helm
storage.mail_crypt.private_keys- Added
- 2.4.8
File of age identities that open stored mail, one per line; during a key rotation it holds the old key and the new one. Read when it exists, so a deployment that stopped writing encrypted mail still reads it.
reference_keys
- Default
[]- Type
- list
- Helm
storage.mail_crypt.reference_keys- Added
- 2.4.8
PEM private keys (P-256) that open bodies of a store taken over from another server in the elliptic-curve encrypted stream format. Every file listed is required at start.
reference_cache_size
- Default
256M- Type
- string
- Helm
storage.mail_crypt.reference_cache_size- See also
mail_temp_dir- Added
- 2.4.8
Decrypted bodies in that format kept in mail_, summed per process; past it a body is read as a stream. 0 keeps none.
mail_temp_dir
- Default
/tmp/yarilo-mail- Type
- string
- Helm
storage.mail_temp_dir- Added
- 2.4.8
Where a process puts plaintext it must spill, such as decrypted bodies of the cache above. Never on the mail volume.
From the rollout on, new mail is encrypted. Mail already on disk stays as it was until it is re-encrypted.
Re-encrypting stored mail
To see where a user's mail stands, preview a run, run it, and then do the same for every user:
yarctl backend crypt status alice@example.com
yarctl backend crypt reencrypt alice@example.com --dry-run
yarctl backend crypt reencrypt alice@example.com
yarctl backend crypt reencrypt --all --jobs 2 --rate 100
yarctl backend crypt status --allstatus counts each folder's messages by state:
| State | Meaning |
|---|---|
plain | stored in clear |
current | age, sealed to exactly the keys of public_ |
other_ | age that another configured key opens, alone or beside the current one |
reference | the other server's format, opened by reference_ |
no_ | encrypted, and no configured key opens it |
reencrypt rewrites every message that is not current (or, with --to plain, every encrypted one) and leaves no_ messages untouched. Each message is written to a temporary file, read back and compared with the old plaintext by SHA-256, and only then put in place; a message that fails any step stays as it was and is counted. Name, flags, UID, date, GUID and sizes do not change, and the search index is not rebuilt.
--rate Nbounds the messages rewritten a second, shared by the jobs running at once (default 100); 0 is no bound.--alltakes every user from the user database,--jobsat a time, each on the backend that keeps the user, then the public namespace once (--public NAME, defaultpublic). The summary sums every state over all of them and exits non-zero when anything failed. An account that has no mail home yet, one that never logged in or received mail, is skipped and counted apart; a home that exists but cannot be read is a failure.- An mdbox store is rewritten whole, one storage file at a time, so a run on it names no folders.
- A job runs in yarilo-backend-api, one re-encryption per user at a time; a
statusreads beside it, so a rotation's progress can be asked either way.crypt job <user>shows the job started last. A restart of that pod loses the job; run the command again, and it continues where the mail shows it was: every message is classified anew. - Run right after such a restart, the command waits up to 80 s for the lost job's hold to lapse, then starts. While a re-encryption for the same user still runs on another backend, a second one is refused:
a crypt job for <user> is already running on another backend. - Ctrl-C cancels the running jobs; a second Ctrl-C leaves at once.
reencrypt needs the lock service (yarilo-locks): without it the command is refused at once. On maildir it also needs flock on the mail volume: without it the job stops at its first message with the reason nolock and writes nothing. status and --dry-run need neither.
Back to plaintext
To store mail in clear again, decrypt it first:
yarctl backend crypt reencrypt --all --to plainthen set write: off and roll out. Keep the private keys until status --all shows no encrypted message.
Key rotation
- Add the new private key to the
privatekey of the Secret, beside the old one. - Switch
publicto the new recipient; roll out. yarctl backend crypt reencrypt --all.yarctl backend crypt status --allshows 0other_and 0key referencefor every user and the public namespace. Waitjmap_after step 2, so that JMAP uploads sealed to the old key have expired.upload_ ttl - Remove the old private key; roll out. Everything reads.
Moving from X25519 to a hybrid key, or back, is the same procedure.
Mount requirements
maildir re-encryption locks each folder with flock (storage_, the default) shared by the sessions and exclusive by the swap. On NFS the lock must be taken on the server: a mount with local_, local_ or nolock gives a flock that other hosts do not see. Do not run maildir re-encryption on such a mount. The uid list of maildir has relied on the same lock all along.
A volume out of space or inodes still lets mail be removed and flags change; a re-encryption there does not run.
Metrics
| Metric | Labels | Meaning |
|---|---|---|
mailbox_ | driver | bodies encrypted on save |
mailbox_ | driver, format | bodies opened: plain, age, reference |
mailbox_ | driver, op, reason | bodies refused: no-key, integrity, truncated, unsupported, planted |
mailbox_ | op, format | time to seal, or to open up to the first byte |
mailbox_ | result | file keys found in the key cache (hit) or unwrapped (miss) |
mailbox_ | decrypted reference bodies held in mail_ | |
mailbox_ | reference bodies read as a stream because the cache was full | |
mailbox_ | driver, from | messages rewritten, by the state they were in |
mailbox_ | driver, reason | messages left as they were |
mailbox_ | driver | messages no configured key opens |
maildir_ | mode, waited | holds of the maildir folder lock; waited="no-room" is a rename that went unheld on a full volume |
Reasons a re-encryption leaves a message: verify (the new body did not read back the same), read, write, nospace (the run stops), vanished (expunged meanwhile), changed (renamed or moved meanwhile; a rerun takes it), busy (the folder stayed busy for 2 s; a rerun takes it) and nolock (no flock on the volume; the run stops).
Cost
Measured on a test cluster with imaptest, the same load with encryption off and on, X25519 and hybrid keys: logins per run fell by 16 to 21 % with encryption on (mdbox 21 %, maildir 16 %, sdbox 20 %). Read this as an upper bound: the test machine's CPU has no SHA extensions and ran at 91 to 95 % in every run, where a percent of CPU costs more than a percent of throughput. Delivery over LMTP did not slow down measurably.
On disk, an age message in mdbox takes about 1.7 KB more; in maildir and sdbox the growth hides in the filesystem's block rounding.
Limits
- There are no folder or user keys: every user's mail is sealed to the same global keys.
- Encryption covers maildir, sdbox and mdbox; other storage formats store plaintext.