matcha icon indicating copy to clipboard operation
matcha copied to clipboard

PGP decryption always fails on server-side encrypted mail (raw armor passed to pgpmail.Read)

Open soyomarvaldezg opened this issue 2 months ago • 1 comments

Summary

PGP decryption of incoming mail fails with failed to decrypt PGP message: message: malformed MIME header line: -----BEGIN PGP MESSAGE- whenever the encryption was applied server-side (e.g. mailbox.org's "Encrypted Inbox" Sieve rule). Decryption of matcha-sent PGP mail works; server-encrypted mail never works. The root cause is in decryptPGPMessage in fetcher/fetcher.go.

Environment

  • matcha 0.44.0 (also reproduced on 0.39.0; still present in v1.0.0-rc2)
  • macOS (Apple Silicon), Homebrew cask
  • Provider: mailbox.org with "PGP encryption for incoming mails" (Sieve auto-encrypts inbound mail to the account's public key, RFC 3156 PGP/MIME)

Reproduction

  1. Enable mailbox.org Encrypted Inbox (Sieve encrypts inbound mail to your public key).

  2. Receive any email. On the server it is stored as multipart/encrypted with an application/octet-stream part containing the ASCII-armored -----BEGIN PGP MESSAGE----- block.

  3. Open the message in matcha. It always shows:

    PGP Decryption Failed: failed to decrypt PGP message: message: malformed MIME
    header line: -----BEGIN PGP MESSAGE-
    
  4. The same message decrypts cleanly with gpg --decrypt using the same private key, so the key, passphrase, and ciphertext are fine.

Root cause

decryptPGPMessage (fetcher/fetcher.go, ~L2091-2134) is called with the bare armored payload extracted from the application/octet-stream part (see the part-walk in checkPart, ~L1042-1102). It then passes those raw bytes straight to:

mr, err := pgpmail.Read(bytes.NewReader(encryptedData), openpgp.EntityList{entityList[0]}, nil, nil)

pgpmail.Read (emersion/go-pgpmail v0.2.2) expects a complete RFC 822 MIME entity — it runs textproto.ReadHeader on the input first. The armor's first line -----BEGIN PGP MESSAGE----- contains no colon, so go-message returns malformed MIME header line: -----BEGIN PGP MESSAGE----- and decryption aborts before the private key is ever used.

Suggested fix (either approach)

  1. Armor path: if the payload starts with -----BEGIN PGP MESSAGE-----, decode with openpgp.ReadArmoredMessage and decrypt with openpgp.Decrypt directly, then feed the plaintext to mail.CreateReader (the caller already does this parse); or
  2. MIME path: synthesize a minimal RFC 3156 wrapper around the extracted part (multipart/encrypted + application/pgp-encrypted version part + the octet-stream part) and pass that to pgpmail.Read.

Why this matters

Server-side encryption (mailbox.org Sieve) is currently the only practical way to get encrypted mail at rest over open standards, since it requires no sender-side PGP. Any matcha user with mailbox.org's Encrypted Inbox hits this on every single message.


Diagnosis and root-cause analysis assisted by Crush (CLI AI agent, model GLM by Z.ai). Reproduction environment and verification (gpg round-trip test on the same key pair) performed on the reporter's machine.

soyomarvaldezg avatar Sep 10 '26 03:09 soyomarvaldezg

Fixed on master!

andrinoff avatar Oct 04 '26 09:10 andrinoff