Kryptografie/Mailserver

Aus Foxwiki

Mailservers

This section documents the most common mail servers. Mail servers may usually be grouped into three categories:

  • Mail Submission Agent (MSA)
  • Mail Transfer Agent (MTA) / Mail Exchanger (MX)
  • Mail Delivery Agent (MDA)

An email client (mail user agent, MUA) submits mail to the MSA. This is usually been done using the Simple Mail Transfer Protocol (SMTP). Afterwards, the mail is transmitted by the MTA over the Internet to the MTA of the receiver. This happens again via SMTP. Finally, the mail client of the receiver will fetch mail from an MDA usually via the Internet Message Access Protocol (IMAP) or the Post Office Protocol (POP). As MSAs and MTAs both use SMTP as transfer protocols, both functionalities may often be implemented with the same software. On the other hand, MDA software might or might not implement both IMAP and POP.

TLS usage in mail server protocols

Email protocols support TLS in two different ways. It may be added as a protocol wrapper on a different port. This method is referred to as Implicit TLS or as protocol variants SMTPS, IMAPS and POP3S. The other method is to establish a cleartext session first and switch to TLS afterwards by issuing the STARTTLS command. SMTP between MTAs usually makes use of opportunistic TLS. This means that an MTA will accept TLS connections when asked for it but will not require it. MTAs should always try opportunistic TLS handshakes outgoing and always accept incoming opportunistic TLS.

Recommended configuration

We recommend to use the following settings for Mail Transfer Agents:* correctly setup MX, A and PTR RRs without using CNAMEs at all

  • the hostname used as HELO/EHLO in outgoing mail shall match the PTR RR
  • enable opportunistic TLS, using the STARTTLS mechanism on port 25
  • Implicit TLS on port 465 may be offered additionally
  • use server and client certificates (most server certificates are client certificates as well)
  • either the common name or at least an alternate subject name of the certificate shall match the PTR RR (client mode) or the MX RR (server mode)
  • do not use self signed certificates
  • accept all cipher suites, as the alternative would be to fall back to cleartext transmission
  • an execption to the last sentence is that MTAs MUST NOT enable SSLv2 protocol support, due to the DROWN attack.

For MSA operation we recommend:* listen on submission port 587 with mandatory STARTTLS

  • optionally listen on port 465 with Implicit TLS
  • enforce SMTP AUTH even for local networks
  • ensure that SMTP AUTH is not allowed on unencrypted connections
  • only use the recommended cipher suites if all connecting MUAs support them

For MDA operation we recommend:* listen on the protocol port (143 for IMAP, 110 for POP3) with mandatory STARTTLS

  • optionally listen on Implicit TLS ports (993 for IMAPS, 995 for POP3S)
  • enforce authentication even for local networks
  • make sure that authentication is not allowed on unencrypted connections
  • use the recommended cipher suites if all connecting MUAs support them
  • turn off SSLv2 (see: DROWN attack)

Dovecot

Tested with Versions

Table 4. Tested Dovecot Versions

Program Version OS/Distribution/Version Comment
2.1.7 Debian Wheezy without ssl_prefer_server_ciphers
2.2.9 Debian Jessie
2.2.13 Debian 8.2 Jessie
2.0.19apple1 OS X Server 10.8.5 without ssl_prefer_server_ciphers
2.2.9 Ubuntu 14.04 Trusty
2.2.31 Ubuntu 16.04.3 LTS

Settings

Dovecot SSL Configuration
# SSL protocols to use, disable SSL, use TLS only
ssl_protocols = !SSLv3 !SSLv2
# SSL ciphers to use
ssl_cipher_list = EDH+CAMELLIA:EDH+aRSA:EECDH+aRSA+AESGCM:EECDH+aRSA+SHA256:EECDH:+CAMELLIA128:+AES128:+SSLv3:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!DSS:!RC4:!SEED:!IDEA:!ECDSA:kEDH:CAMELLIA128-SHA:AES128-SHA
# Prefer the server's order of ciphers over client's. (Dovecot >=2.2.6 Required)
ssl_prefer_server_ciphers = yes
# Diffie-Hellman parameters length (Default is 1024, Dovecot >=2.2.7 Required)
# ToDo: for ReGenerating DH-Parameters:
# manually delete /var/lib/dovecot/ssl-parameters.dat and restart
# Dovecot to regenerate /var/lib/dovecot/ssl-parameters.dat
ssl_dh_parameters_length = 2048
# Disable Compression (Dovecot >= 2.2.14 Required)
# ssl_options = no_compression
Dovecot 2.0, 2.1: Almost as good as dovecot 2.2. Dovecot does not ignore unknown configuration parameters. Does not support ssl_prefer_server_ciphers.

Limitations

Dovecot <2.2.14 does not support disabling TLS compression.

  • In >2.2.14 [6] use: ssl_options = no_compression

Dovecot <2.2.7 uses fixed DH parameters.

  • In >2.2.7 [7] greater DH-Parameters are supported: ssl_dh_parameters_length = 2048.

References

How to test

$ openssl s_client -crlf -connect example.com:993
$ openssl s_client -crlf -connect example.com:995
$ openssl s_client -crlf -starttls imap -connect example.com:143
$ openssl s_client -crlf -starttls pop3 -connect example.com:110

SSLyze offers scanning for common vulnerabilities and displays Protocols and Cipher-Suites.

$ sslyze.exe --regular example.com:993
$ sslyze.exe --regular example.com:995
$ sslyze.exe --regular --starttls=imap example.com:143
$ sslyze.exe --regular --starttls=pop3 example.com:110

Postfix

Tested with Versions

Tested Postfix Versions

Program Version OS/Distribution/Version Comment
2.9.6 Debian Wheezy with OpenSSL 1.0.1e
2.11.0 Ubuntu 14.04.02 with OpenSSL 1.0.1f
3.1.0 Ubuntu 16.04.3 LTS

Settings

Postfix has five internal lists of ciphers, and the possibility to switch between those with smtpd_tls_ciphers. However, we leave this at its default value for server to server connections, as many mail servers only support outdated protocols and ciphers. We consider bad encryption still better than plain text transmission. For connections to MUAs, TLS is mandatory and the ciphersuite is modified.

MX and SMTP client configuration:

As discussed in section [smtp_general], because of opportunistic encryption we do not restrict the list of ciphers or protocols for communication with other mail servers to avoid transmission in plain text. There are still some steps needed to enable TLS, all in main.cf:

Opportunistic TLS in Postfix
# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
# log TLS connection info
smtpd_tls_loglevel = 1
smtp_tls_loglevel = 1
# enable opportunistic TLS support in the SMTP server and client
smtpd_tls_security_level = may
smtp_tls_security_level = may
# if you have authentication enabled, only offer it after STARTTLS
smtpd_tls_auth_only = yes
tls_ssl_options = NO_COMPRESSION
MSA

For the MSA smtpd process which communicates with mail clients, we first define the ciphers that are acceptable for the “mandatory” security level, again in main.cf:

MSA TLS configuration in Postfix
smtp_tls_mandatory_protocols = !SSLv2, !SSLv3
smtp_tls_protocols = !SSLv2, !SSLv3
lmtp_tls_mandatory_protocols = !SSLv2, !SSLv3
lmtp_tls_protocols = !SSLv2, !SSLv3
smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_protocols = !SSLv2, !SSLv3
smtpd_tls_mandatory_ciphers=high
tls_high_cipherlist=EDH+CAMELLIA:EDH+aRSA:EECDH+aRSA+AESGCM:EECDH+aRSA+SHA256:EECDH:+CAMELLIA128:+AES128:+SSLv3:!aNULL:!eNULL:!LOW:!3DES:!MD5:!EXP:!PSK:!DSS:!RC4:!SEED:!IDEA:!ECDSA:kEDH:CAMELLIA128-SHA:AES128-SHA

Then, we configure the MSA smtpd in master.cf with two additional options that are only used for this instance of smtpd:

MSA smtpd service configuration in Postfix
# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (no)    (never) (100)
# ==========================================================================
# ...
submission inet n       -       -       -       -       smtpd
   -o smtpd_tls_security_level=encrypt
   -o tls_preempt_cipherlist=yes
# ...

For those users who want to use EECDH key exchange, it is possible to customize this via: The default value since Postfix 2.8 is “strong”.

EECDH customization in Postfix
smtpd_tls_eecdh_grade = ultra

Limitations

tls_ssl_options is supported from Postfix 2.11 onwards.

  • You can leave the statement in the configuration for older versions, it will be ignored.

tls_preempt_cipherlist is supported from Postfix 2.8 onwards.

  • Again, you can leave the statement in for older versions.

References

Additional settings

Postfix has two sets of built-in DH parameters that can be overridden with the smtpd_tls_dh512_param_file and smtpd_tls_dh1024_param_file options. The “dh512” parameters are used for export ciphers, while the “dh1024” ones are used for all other ciphers. The “bit length” in those parameter names is just a name, so one could use stronger parameter sets; it should be possible to e.g. use the IKE Group14 parameters (see section [DH] without much interoperability risk, but we have not tested this yet.

How to test

You can check the effect of the settings with the following command:

$ zegrep "TLS connection established from.*with cipher" /var/log/mail.log | awk '{printf("%s %s %s %s\n", $12, $13, $14, $15)}' | sort | uniq -c | sort -n
     1 SSLv3 with cipher DHE-RSA-AES256-SHA
    23 TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384
    60 TLSv1 with cipher ECDHE-RSA-AES256-SHA
   270 TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384
   335 TLSv1 with cipher DHE-RSA-AES256-SHA
$ openssl s_client -starttls smtp -crlf -connect example.com:25

Exim

Tested with Versions

Table 7. Tested Exim Versions

Program Version OS/Distribution/Version Comment
4.82 Debian Jessie
4.82 Ubuntu 14.04.2 with OpenSSL 1.0.1e

It is highly recommended to read Encrypted SMTP connections using TLS/SSL first.

MSA mode (submission):

In the main config section of Exim add:

Certificate selection in Exim (MSA)

tls_certificate = /etc/ssl/exim.crt tls_privatekey = /etc/ssl/exim.pem Don’t forget to add intermediate certificates to the .pem file if needed. Tell Exim to advertise STARTTLS in the EHLO answer to everyone:

TLS advertise in Exim (MSA)

tls_advertise_hosts = * If you want to support legacy SMTPS on port 465, and STARTTLS on smtp(25)/submission(587) ports set

STARTTLS and SMTPS in Exim (MSA)

daemon_smtp_ports = smtp : smtps : submission tls_on_connect_ports = 465 It is highly recommended to limit SMTP AUTH to SSL connections only. To do so add

SSL-only authentication in Exim (MSA)

server_advertise_condition = ${if eq{$tls_cipher}{}{no}{yes}} to every authenticator defined. Add the following rules on top of your acl_smtp_mail:

Submission mode in Exim (MSA)

acl_smtp_mail = acl_check_mail acl_check_mail:

 warn hosts = *
   control = submission/sender_retain
 accept

This switches Exim to submission mode and allows addition of missing “Message-ID” and “Date” headers. It is not advisable to restrict the default cipher list for MSA mode if you don’t know all connecting MUAs. If you still want to define one please consult the Exim documentation or ask on the exim-users mailinglist. The cipher used is written to the logfiles by default. You may want to add log_selector = <whatever your log_selector already contains> +tls_certificate_verified +tls_peerdn +tls_sni to get even more TLS information logged.

Server mode (incoming):

In the main config section of Exim add:

Certificate selection in Exim (Server)

tls_certificate = /etc/ssl/exim.crt tls_privatekey = /etc/ssl/exim.pem Don’t forget to add intermediate certificates to the .pem file if needed. Tell Exim to advertise STARTTLS in the EHLO answer to everyone:

TLS advertise in Exim (Server)

tls_advertise_hosts = * Listen on smtp(25) port only:

STARTTLS on SMTP in Exim (Server)

daemon_smtp_ports = smtp It is not advisable to restrict the default cipher list for opportunistic encryption as used by SMTP. Do not use cipher lists recommended for HTTPS! If you still want to define one please consult the Exim documentation or ask on the exim-users mailinglist. If you want to request and verify client certificates from sending hosts set

TLS certificate verification in Exim (Server)

tls_verify_certificates = /etc/pki/tls/certs/ca-bundle.crt tls_try_verify_hosts = * tls_try_verify_hosts only reports the result to your logfile. If you want to disconnect such clients you have to use tls_verify_hosts = * The cipher used is written to the logfiles by default. You may want to add log_selector = <whatever your log_selector already contains> +tls_certificate_verified +tls_peerdn +tls_sni to get even more TLS information logged.

Client mode (outgoing):

Exim uses opportunistic encryption in the SMTP transport by default. Client mode settings have to be done in the configuration section of the smtp transport (driver = smtp). If you want to use a client certificate (most server certificates can be used as client certificate, too) set

Certificate selection in Exim (Client)

tls_certificate = /etc/ssl/exim.crt tls_privatekey = /etc/ssl/exim.pem This is recommended for MTA-MTA traffic. Do not limit ciphers without a very good reason. In the worst case you end up without encryption at all instead of some weak encryption. Please consult the Exim documentation if you really need to define ciphers.

OpenSSL:

Exim already disables SSLv2 by default. We recommend to add openssl_options = +all +no_sslv2 +no_sslv3 +no_compression +cipher_server_preference to the main configuration.

+all is misleading here since OpenSSL only activates the most common workarounds. But that’s how SSL_OP_ALL is defined.

You do not need to set dh_parameters. Exim with OpenSSL by default uses parameter initialization with the “2048-bit MODP Group with 224-bit Prime Order Subgroup” defined in section 2.2 of RFC 5114 (ike23). If you want to set your own DH parameters please read the TLS documentation of exim.

GnuTLS:

GnuTLS is different in only some respects to OpenSSL:* tls_require_ciphers needs a GnuTLS priority string instead of a cipher list. It is recommended to use the defaults by not defining this option. It highly depends on the version of GnuTLS used. Therefore it is not advisable to change the defaults.

  • There is no option like openssl_options
Exim string expansion

Most of the options accept expansion strings. This way you can e.g. set cipher lists or STARTTLS advertisement conditionally. Please follow the link to the official Exim documentation to get more information.

Limitations:

Exim currently (4.82) does not support elliptic curves with OpenSSL. This means that ECDHE is not used even if defined in your cipher list. There already is a working patch to provide support.

How to test

$ openssl s_client -starttls smtp -crlf -connect example.com:25

Weblinks

  1. https://bettercrypto.org/