Although Postfix (and the SMTP protocol in general) can function without any kind of encryption, enabling TLS it can be a good idea in terms of both security and privacy, so let’s look at how it can be easily done.
We’ll actually be configuring two separate types of encryption:
- Opportunistic encryption for regular SMTP (port 25), both incoming 1 and outgoing 2. “Opportunistic”, here, means that the server will ask for encryption and use it if the other side also supports it, but if it doesn’t then it’ll work without encryption. Although forcing it might sound tempting for security reasons, in reality many “smaller” servers around the world still don’t support it, so you would be unable to send or receive mail to/from those. Opportunistic TLS at least means that most “big” email services (e.g. Gmail) will communicate with you (and you with them) with encryption.
- Forced encryption for the submission service (port 587). This is used by your own users (even if it’s just you) to send mail through your server, and typically has different restrictions (e.g. it’s authenticated, but on the other hand it can be used to send mail to the outside (unlike incoming port 25 which doesn’t, as that would make it an open relay)). Both the users’ authentication that would otherwise be in clear text, and the fact that only a limited number of users (your users, too) will be accessing that port through email clients (never other email servers that you don’t control) make forcing encryption here a no-brainer.
NOTE: naturally, this is not an exhaustive Postfix tutorial. I’ll be assuming you already have a working server, and just want to add TLS encryption to it. Also, some parts may not apply to your case.
1. Have/get a TLS certificate for your email host’s public name
If you don’t already have one, see how to create a new TLS certificate. We’ll be using an RSA certificate here (instead of going with ECDSA) since unlike, say, a web server accessed by standard browsers, you can’t control what other email servers support 3, therefore it’s best to choose the most common option. 4
Note that the certificate’s CN must match your email host’s public name (e.g. “mail.domain.com“). That will also probably correspond to (one of) the domain’s MX records.
2. Postfix configuration
Again, I’ll be assuming your non-TLS Postfix is already working fine.
Put your certificate and key in /etc/postfix (for instance). For this example, I’ll be calling them myserver-full.crt and myserver.key.
In /etc/postfix/main.cf, add the following lines:
And in /etc/postfix/master.cf, assuming you already have a “submission” section a bit like this:
Add to that section:
The above forces encryption for the submission service (remember that it’s not required for normal SMTP, it’s just desired).
- your server tries to connect to others using TLS, but falls back to an unencrypted connection if the other side doesn’t support encryption;
- other servers can connect to yours using TLS, but if they don’t attempt it then the connection will be unencrypted;
- your users *must* use encryption (and authentication) to send mail through your server.
Restart your server, and check the logs: you should be getting mentions of TLS now. For instance, an email server starting an encrypted connection to yours looks like:
Sep 6 14:25:58 drax postfix/smtpd: Anonymous TLS connection established from lists.openbsd.org[18.104.22.168]: TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)
(And what about securing mailbox access? Enjoy: How to configure TLS encryption in Dovecot)