Skip to content
Documentation Background

Email security

Email was designed without security. A message crosses several servers in the clear, and the From: line is just text. Three separate layers of protection exist today, and they answer different questions. Mixing them up is the usual reason people think a message is “secure” when it is not.

LayerProtectsMechanism
TransportA message on one hop between servers, or between client and serverTLS on SMTP, IMAP, and POP
Domain authenticationThe claim that a domain sent the messageSPF, DKIM, DMARC
End-to-endThe message content, from the sender to the recipient’s mailboxPGP or S/MIME

Only end-to-end protection hides the content from the mail servers in the middle. How email works is covered with SMTP and DNS.


RequirementMeaning
ConfidentialityOnly the recipient can read the message, not the servers carrying it
Sender authenticationThe recipient can be sure who wrote it
Message integrityThe recipient can tell if it was changed
Non-repudiationThe sender cannot credibly deny sending it (a signature supports this)

The building blocks are those in cryptography fundamentals: a hash for a compact fingerprint, a private-key signature for authentication and integrity, symmetric encryption with a one-time key for the bulk of the message, and public-key encryption to deliver that key.


Confidentiality. Public-key encryption of a long message is slow, so the sender uses the hybrid approach:

  1. Generate a random one-time session key Ks.
  2. Encrypt the message with Ks using a symmetric cipher.
  3. Encrypt Ks with the recipient’s public key.
  4. Send both. Only the recipient’s private key recovers Ks, and then the message.

Integrity and authentication. Signing the whole message with a private key works but is slow and large. Instead:

  1. Hash the message (for example SHA-256).
  2. Sign the hash with the sender’s private key.
  3. Send the message plus the signature. The recipient hashes the message, verifies the signature with the sender’s public key, and compares.

Both. Sign first, then encrypt the message and signature with a session key, and send the wrapped session key. The result is one package:

signature = Sign( private key of sender, Hash(message) )
package = SessionKeyEncrypt( Ks, message + signature ) + PublicKeyEncrypt( recipient key, Ks )

The recipient reverses it: unwrap Ks, decrypt, then verify the signature.

Trust in the keys is the hard part. A signature is only as good as your knowledge that the public key belongs to the sender. S/MIME gets that from X.509 certificates issued by certificate authorities, as in PKI and certificates. PGP uses a web of trust, in which people sign each other’s keys. Both are covered in GPG and file encryption.


Both give the same four properties and use the same shape: hash, sign, session key, wrapped key. The differences are in key trust and tooling.

PGP / OpenPGPS/MIME
Key trustWeb of trust, key servers, direct fingerprint checksCertificate authority hierarchy
Typical usersDevelopers, activists, open-source projectsEnterprises, government
Client supportPlugins and some mail clientsBuilt into most mail clients
Key lifecycleUser-managedIssued and revoked centrally

Neither is widely used by general users. The usual reasons are key management, lost keys that cannot be recovered, and that search, spam filtering, and webmail all need to read content. Many organizations rely on transport security and domain authentication, and add end-to-end encryption only for specific high-risk exchanges, or use secure portals instead.

Signing git commits and release artifacts uses the same primitives, and is where most engineers meet them today.

Terminal window
# Check that a signed email's signature verifies (message saved to msg.eml)
gpg --verify msg.sig msg.txt
# S/MIME: verify with OpenSSL
openssl smime -verify -in msg.eml -CAfile ca-bundle.crt

SMTP began as plain text. Servers upgrade it with STARTTLS: the connection starts in the clear, the client asks to switch to TLS, and both proceed encrypted. The weakness is that the upgrade is opportunistic. An attacker who strips the STARTTLS offer forces cleartext, and many servers will accept that.

Terminal window
# See whether a mail server offers STARTTLS and what certificate it presents
openssl s_client -starttls smtp -connect mail.example.com:25 -servername mail.example.com

Stronger policies tell sending servers that TLS is required. MTA-STS publishes the policy over HTTPS and DNS, and DANE publishes certificate information in DNSSEC-signed DNS records. Clients submitting mail use the dedicated submission port (587 with STARTTLS, or 465 with implicit TLS) with authentication.

Even with perfect transport encryption, each receiving server reads the message and stores it. Transport security protects the wire, not the mailbox.


SPF, DKIM, and DMARC answer “did this domain really send it?”, not “who wrote it?” and not “was it kept private?”. SPF lists the servers allowed to send for a domain. DKIM has the sending server sign selected headers and the body with a key published in DNS. DMARC sets what a receiver does when those checks do not align with the visible From: domain. They are covered in Social engineering attacks, where they matter most, and they should be on every domain that sends mail.

Terminal window
dig +short TXT example.com | grep spf
dig +short TXT _dmarc.example.com
dig +short TXT selector1._domainkey.example.com