# Encryption plan: OpenPGP mail encryption for ep-mail Status: **Phase 1 (foundation) is implemented on desktop.** Phases 2–6 are tracked in [TODO.md](./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](https://github.com/openpgpjs/openpgpjs) (`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 with modern defaults. - Also detect and decrypt legacy inline PGP (`-----BEGIN PGP MESSAGE-----`). - Out of scope for v1: Autocrypt headers, key servers, Web Key Directory. ## 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, 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 | Planned | | 3 | Send: public-key keyring and contact import, encrypt and sign on compose, encrypted Sent copy, downgrade warnings | Planned | | 4 | Trust and polish: fingerprint verification, key-change warnings, passphrase cache timeout, Android Keystore and biometric unlock | Planned | | 5 | Interop testing with Thunderbird and GnuPG, large attachments, multiple recipients, corrupted payloads | Planned | | 6 | Docs, security review, release | Planned | ## 6. Phase 1 implementation notes - [desktop/pgp-keystore.cjs](./desktop/pgp-keystore.cjs): platform-independent key store with an injected storage backend. - [desktop/main.cjs](./desktop/main.cjs): wraps the store with `safeStorage` (`pgp-keys.enc` in the user data folder) and exposes `pgp:*` IPC handlers; key files are saved through native dialogs. - [desktop/preload.cjs](./desktop/preload.cjs): exposes `daybreakMail.pgp`. - [pgp-ui.js](./pgp-ui.js): Settings → **Encryption keys**. Hidden on platforms without a key store (Android until Phase 4). - Tests: `npm test` ([tests/pgp-keystore.test.cjs](./tests/pgp-keystore.test.cjs)). ## 7. 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.