Files
ep-mail-client/ENCRYPTION_PLAN.md
T
thuttuandCopilot 04cd4117fe Add OpenPGP public key server support
- Add VKS and HTTPS HKP lookup and publication with bounded, validated requests.
- Add desktop key-server settings, lookup/import flow, and per-publication native confirmation.
- Keep discovered keys untrusted and update architecture, encryption, and user documentation.
- Add mocked server protocol and key-store tests.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-10 19:50:01 +03:00

8.8 KiB
Raw Blame History

Encryption plan: OpenPGP mail encryption for ep-mail

Status: Phases 1–3 and the desktop parts of 4–6 are implemented. Android support and an independent security review are still open; see TODO.md.

1. Format: OpenPGP with PGP/MIME (RFC 3156)

Option Pros Cons
OpenPGP Interoperates with Thunderbird, Proton, Mailvelope, GnuPG and K-9/OpenKeychain. No certificate authority needed. Key verification is manual.
S/MIME Native in Outlook and Apple Mail. Needs CA-issued certificates; poor fit for personal accounts.

Decision: OpenPGP only for v1; S/MIME may follow later.

  • Library: OpenPGP.js (openpgp), pure JS, usable in Electron and the Android WebView.
  • Messages: multipart/encrypted (application/pgp-encrypted + application/octet-stream), signed and encrypted together. Attachments go inside the encrypted payload.
  • Keys: Curve25519 (legacy EdDSA/ECDH, OpenPGP v4). OpenPGP.js' default native Ed25519 keys are rejected by GnuPG 2.5, so the widely supported format is used.
  • Also detect and decrypt legacy inline PGP (-----BEGIN PGP MESSAGE-----).
  • Out of scope for v1: Autocrypt headers and Web Key Directory. Desktop key-server lookup and publication are described in section 9.

2. Key handling

Private keys

  • One or more keys per account; never stored unprotected and never sent anywhere.
  • Always protected by a passphrase (minimum 8 characters). On desktop the whole key store is also encrypted with Electron safeStorage (the OS credential store).
  • Android: wrap with a hardware-backed Android Keystore key; optional biometric unlock (Phase 4).
  • The passphrase is cached only in memory for a configurable time, then wiped (Phase 4). It is never written to disk.
  • Export as ASCII-armored files; a revocation certificate is generated when a key is created and can be exported after unlocking.
  • Deleting or exporting a private key requires the passphrase.

Public keys

  • Per-account contact keyring: address, fingerprint, trust level, source.
  • Sources: file import, attached to received mail, key-server lookup, and own key attached to outgoing mail.
  • Trust: trust on first use, shown with the fingerprint, plus an explicit "verified" state after out-of-band comparison. Warn loudly when a known contact's key changes.

3. User experience

  • Compose: lock toggle, on by default when every recipient has a key. Never silently downgrade; offer to send unencrypted or cancel.
  • Reader: status line such as "Encrypted and signed by X (verified)" or a warning for bad/unknown signatures.
  • Message list: lock icon for encrypted mail.
  • Settings: Encryption keys (Phase 1) for create, import, export, delete.
  • Interactions: search and rules cannot read encrypted bodies (subject/sender stay plaintext); do not cache decrypted bodies; run link warnings and attachment previews only after decryption; encrypt the Sent copy to the sender's own key too.

4. Threat model and limits

  • Protects content from the mail server and network observers.
  • Does not protect metadata (sender, recipients, subject, timestamps).
  • A compromised device, or a stolen key plus passphrase, defeats it.
  • Losing the key means losing access to old encrypted mail; backups are the user's responsibility.

5. Phases

Phase Scope Status
1 Foundation: OpenPGP.js, passphrase-protected key store (OS credential store on desktop), generate/import/export/delete, revocation certificate, unit tests, Settings UI Done (desktop)
2 Receive: detect PGP/MIME and inline PGP, decrypt on open, verify signatures, status UI, missing-key and wrong-passphrase handling Done (desktop)
3 Send: public-key keyring and contact import, encrypt and sign on compose, encrypted Sent copy, downgrade warnings Done (desktop)
4 Trust and polish: fingerprint verification, key-change warnings, passphrase cache timeout, Android Keystore and biometric unlock Desktop done; Android open
5 Interop testing with Thunderbird and GnuPG, large attachments, multiple recipients, corrupted payloads Automated, GnuPG and manual Thunderbird interop checks done
6 Docs, security review, release Docs and release done; independent review open

6. Phase 1 implementation notes

7. Phases 2–5 implementation notes

  • desktop/pgp-mail.cjs: detects PGP/MIME and inline PGP, decrypts, verifies signatures (inside encrypted mail, multipart/signed and cleartext), builds encrypted/signed PGP/MIME messages, and keeps an in-memory passphrase cache.
  • desktop/pgp-keystore.cjs: also stores contact public keys with trust levels (unknown, trusted, verified) and the passphrase-memory setting.
  • Encrypted messages appear in the list as "Encrypted message"; they are decrypted on demand (pgp:decrypt-message) and the plaintext is never cached on disk. Attachments are read from the decrypted payload.
  • Compose shows Encrypt and Sign options. Encrypt turns on by default when every recipient has a key; if a key is missing the app asks before sending unencrypted. Encryption happens before the message is sent or queued offline, so passphrases and plaintext never enter the outbox. The Sent copy is also encrypted to the sender's own key.
  • A signature from a key that differs from the one on file for the sender is flagged as a changed key.
  • Interop: tests/pgp-gnupg-interop.test.cjs runs against a real GnuPG (skipped when it is not installed).
  • Tests: tests/pgp-mail.test.cjs (round trips, several recipients, tampering, key changes, large attachments, inline PGP).
  • Limits: subjects and headers are not encrypted; drafts are stored unencrypted; opening encrypted attachments requires the passphrase to be remembered (see Settings → Encryption keys).

8. Risks

  • Android WebView performance with large attachments (may need native handling).
  • Key loss (backup flow, revocation certificate).
  • User confusion about trust; keep states simple: unknown, trusted, verified.
  • Decrypted HTML must not leak through remote content; blocking remote images by default should land first.
  • Security-critical code: have it reviewed by someone with cryptography experience before relying on it.

9. Public key servers (desktop)

The desktop Settings → Encryption keys view can search and publish through:

  • keys.openpgp.org using its VKS API. Email lookup only returns keys whose email addresses have been verified. After an upload, ep-mail requests verification for the key's primary email address and reports whether verification is pending or already complete.
  • keys.mailvelope.com using HKP over HTTPS as documented by the Mailvelope key server. The server sends its own email-verification message after accepting a key upload.
  • Custom servers using an explicitly selected HKP or VKS protocol and an HTTPS base URL. Custom server configuration is saved with the encrypted OpenPGP settings.

Searching is user-triggered; ep-mail does not perform background lookups while composing. Search addresses are disclosed to the selected server. The main process validates returned OpenPGP keys before showing them, and adding a result uses the existing key-replacement confirmation. Discovered keys enter the contact keyring with unknown trust; server publication or lookup is never treated as fingerprint verification.

Every publication requires a native confirmation that identifies the server, email address, and fingerprint and explains that the email/key association becomes public and may be retained. Only the selected key's public material is uploaded; private keys and passphrases are never sent. A successful upload may still require the user to complete email verification before the key becomes searchable. Android key-server support is not included because Android OpenPGP support is not implemented.

Custom servers are limited to HTTPS HKP/VKS endpoints, redirects are rejected, and lookup responses are size-limited. Key-server data remains subject to the server's availability, privacy practices, and verification policy; users should independently compare fingerprints before trusting a key.