TLS_README.html   [plain text]


<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<title>Postfix TLS Support </title>

<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">

</head>

<body>

<h1><img src="postfix-logo.jpg" width="203" height="98" ALT="">Postfix TLS Support
</h1>

<hr>

<h2> WARNING </h2>

<p> By turning on TLS support in Postfix, you not only get the
ability to encrypt mail and to authenticate remote SMTP clients or servers.
You also turn on thousands and thousands of lines of OpenSSL library
code.  Assuming that OpenSSL is written as carefully as Wietse's
own code, every 1000 lines introduce one additional bug into
Postfix.  </p>

<h2> What Postfix TLS support does for you </h2>

<p> Transport Layer Security (TLS, formerly called SSL) provides
certificate-based authentication and encrypted sessions.  An
encrypted session protects the information that is transmitted with
SMTP mail or with SASL authentication.

<p> This document describes a TLS user interface that was introduced
with Postfix version 2.3. Support for an older user interface is
documented in TLS_LEGACY_README, which also describes the differences
between Postfix and the third-party patch on which Postfix version
2.2 TLS support was based.  </p>

<p> Topics covered in this document: </p>

<ul>

<li><a href="#how">How Postfix TLS support works</a>

<li><a href="#build_tls">Building Postfix with TLS support</a>

<li><a href="#server_tls">SMTP Server specific settings</a>

<li> <a href="#client_tls">SMTP Client specific settings</a>

<li><a href="#tlsmgr_controls"> TLS manager specific settings </a>

<li><a href="#problems"> Reporting problems </a>

<li><a href="#credits"> Credits </a>

</ul>

<p> And last but not least, for the impatient: </p>

<ul>

<li><a href="#quick-start">Getting started, quick and dirty</a>

</ul>

<h2><a name="how">How Postfix TLS support works</a></h2>

<p> The diagram below shows the main elements of the Postfix TLS
architecture and their relationships.  Colored boxes with numbered
names represent Postfix daemon programs. Other colored boxes
represent storage elements. </p>

<ul>

<li> <p> The smtpd(8) server implements the SMTP over TLS server
side. </p>

<li> <p> The smtp(8) client implements the SMTP over TLS client
side. </p>

<li> <p> The tlsmgr(8) server maintains the pseudo-random number
generator (PRNG) that seeds the TLS engines in the smtpd(8) server
and smtp(8) client processes, and maintains the TLS session key
cache files. </p>

</ul>

<table>

<tr> <td>Network<tt>-&gt; </tt> </td> <td align="center"
bgcolor="#f0f0ff"> <br> <a href="smtpd.8.html">smtpd(8)</a> <br> &nbsp; </td> <td colspan="2">

<tt> &lt;---seed----<br><br>&lt;-key/cert-&gt; </tt> </td> <td
align="center" bgcolor="#f0f0ff"> <br> <a href="tlsmgr.8.html">tlsmgr(8)</a> <br> &nbsp; </td>
<td colspan="3"> <tt> ----seed---&gt;<br> <br>&lt;-key/cert-&gt;

</tt> </td> <td align="center" bgcolor="#f0f0ff"> <br> <a href="smtp.8.html">smtp(8)</a> <br>
&nbsp; </td> <td> <tt> -&gt;</tt>Network </td> </tr>

<tr> <td colspan="3"> </td> <td align="right"> <table> <tr> <td>

</td> <td> / </td> </tr> <tr> <td> / </td> <td> </td> </tr> </table>
</td> <td align="center"> |<br> |</td> <td align="left"> <table>

<tr> <td> \ </td> <td> </td> </tr> <tr> <td> </td> <td> \ </td>
</tr> </table> </td> <td colspan="3"> </td> </tr>

<tr> <td colspan="2"> </td> <td align="center" bgcolor="#f0f0ff">
smtpd<br> session<br> key cache </td> <td> </td> <td align="center"
bgcolor="#f0f0ff"> PRNG<br> state <br>file </td> <td> </td> <td
align="center" bgcolor="#f0f0ff"> smtp<br> session<br> key cache
</td>

<td colspan="2"> </td> </tr>

</table>

<h2><a name="build_tls">Building Postfix with TLS support</a></h2>

<p> These instructions assume that you build Postfix from source
code as described in the INSTALL document. Some modification may   
be required if you build Postfix from a vendor-specific source
package.  </p>

<p> To build Postfix with TLS support, first we need to generate
the <tt>make(1)</tt> files with the necessary definitions. This is
done by invoking the command "<tt>make makefiles</tt>" in the Postfix
top-level directory and with arguments as shown next. </p>

<p> <b> NOTE: Do not use Gnu TLS.  It will spontaneously terminate
a Postfix daemon process with exit status code 2, instead of allowing
Postfix to 1) report the error to the maillog file, and to 2) provide
plaintext service where this is appropriate.  </b> </p>

<ul>

<li> <p> If the OpenSSL include files (such as <tt>ssl.h</tt>) are
in directory <tt>/usr/include/openssl</tt>, and the OpenSSL libraries
(such as <tt>libssl.so</tt> and <tt>libcrypto.so</tt>) are in
directory <tt>/usr/lib</tt>:  </p>

<blockquote>
<pre>
% <b>make tidy</b> # if you have left-over files from a previous build
% <b>make makefiles CCARGS="-DUSE_TLS" AUXLIBS="-lssl -lcrypto"</b>
</pre>
</blockquote>

<li> <p> If the OpenSSL include files (such as <tt>ssl.h</tt>) are
in directory <tt>/usr/local/include/openssl</tt>, and the OpenSSL
libraries (such as <tt>libssl.so</tt> and <tt>libcrypto.so</tt>)
are in directory <tt>/usr/local/lib</tt>:  </p>

<blockquote>
<pre>
% <b>make tidy</b> # if you have left-over files from a previous build
% <b>make makefiles CCARGS="-DUSE_TLS -I/usr/local/include" \
    AUXLIBS="-L/usr/local/lib -lssl -lcrypto" </b>
</pre>
</blockquote>

<p> On Solaris, specify the <tt>-R</tt> option as shown below:

<blockquote>
<pre>
% <b>make tidy</b> # if you have left-over files from a previous build
% <b>make makefiles CCARGS="-DUSE_TLS -I/usr/local/include" \
    AUXLIBS="-R/usr/local/lib -L/usr/local/lib -lssl -lcrypto" </b>
</pre>
</blockquote>

</ul>

<p> If you need to apply other customizations (such as Berkeley DB
databases, MySQL, PostgreSQL, LDAP or SASL), see the respective
Postfix README documents, and combine their "<tt>make makefiles</tt>"
instructions with the instructions above:  </p>

<blockquote>
<pre>
% <b>make tidy</b> # if you have left-over files from a previous build
% <b>make makefiles CCARGS="-DUSE_TLS \
    <i>(other -D or -I options)</i>" \
    AUXLIBS="-lssl -lcrypto \
    <i>(other -l options for libraries in /usr/lib)</i> \
    <i>(-L/path/name + -l options for other libraries)</i>"</b>
</pre>
</blockquote>

<p> To complete the build process, see the Postfix INSTALL
instructions. Postfix has TLS support turned off by default, so
you can start using Postfix as soon as it is installed.  </p>

<h2><a name="server_tls">SMTP Server specific settings</a></h2>

<p> Topics covered in this section: </p>

<ul>

<li><a href="#server_cert_key">Server-side certificate and private
key configuration </a>

<li><a href="#server_logging"> Server-side TLS activity logging
</a>

<li><a href="#server_enable">Enabling TLS in the Postfix SMTP server </a>

<li><a href="#server_vrfy_client">Client certificate verification</a>

<li><a href="#server_tls_auth">Supporting AUTH over TLS only</a>

<li><a href="#server_tls_cache">Server-side TLS session cache</a>

<li><a href="#server_access">Server access control</a>

<li><a href="#server_cipher">Server-side cipher controls</a>

<li><a href="#server_misc"> Miscellaneous server controls</a>

</ul>

<h3><a name="server_cert_key">Server-side certificate and private
key configuration </a> </h3>

<p> In order to use TLS, the Postfix SMTP server generally needs
a certificate and a private key. Both must be in "PEM" format. The
private key must not be encrypted, meaning:  the key must be accessible
without a password.  The certificate and private key may be in the same
file, in which case the certificate file should be owned by "root" and
not be readable by any other user. If the key is stored separately,
this applies to the key file only, and the certificate file may be
"world-readable". </p>

<p> Public Internet MX hosts without certificates signed by a "reputable"
CA must generate, and be prepared to present to most clients, a
self-signed or private-CA signed certificate. The remote SMTP client
will generally not be
able to authenticate the self-signed certificate, but unless the
client is running Postfix 2.3 or
similar software, it will still insist on a server certificate. </p>

<p> For servers that are <b>not</b> public Internet MX hosts, Postfix
supports configurations with no certificates. This entails the
use of just the anonymous TLS ciphers, which are not supported by
typical SMTP clients. Since such clients will not, as a rule, fall
back to plain text after a TLS handshake failure, a certificate-less
Postfix SMTP server will
be unable to receive email from most TLS enabled clients. To avoid
accidental configurations with no certificates, Postfix enables
certificate-less operation only when the administrator explicitly sets
"smtpd_tls_cert_file = none". This ensures that new Postfix
SMTP server configurations will not accidentally run with no
certificates. </p>

<p> Both RSA and DSA certificates are supported. Typically you will
only have RSA certificates issued by a commercial CA. In addition,
the tools supplied with OpenSSL will by default issue RSA certificates.
You can have both at the same time, in which case the cipher used
determines which certificate is presented. For Netscape and OpenSSL
clients without special cipher choices, the RSA certificate is
preferred. </p>

<p> In order for remote SMTP clients to check the Postfix SMTP
server certificates, the CA certificate (in case of a certificate
chain, all CA certificates) must be available.  You should add any
intermediate CA certificates to the server certificate: the server
certificate first, then the intermediate CA(s).  </p>

<p> Example: the certificate for "server.example.com" was issued by
"intermediate CA" which itself has a certificate issued by "root
CA".  Create the server.pem file with: </p>

<blockquote>
<pre>
% <b>cat server_cert.pem intermediate_CA.pem &gt; server.pem</b>
</pre>
</blockquote>

<p> A Postfix SMTP server certificate supplied here must be usable
as SSL server certificate and hence pass the "openssl verify -purpose
sslserver ..." test. </p>

<p> A client that trusts the root CA has a local copy of the root
CA certificate, so it is not necessary to include the root CA
certificate here.  Leaving it out of the "server.pem" file reduces
the overhead of the TLS exchange. </p>

<p> If you want the Postfix SMTP server to accept remote SMTP client
certificates issued by these CAs, append the root certificate to
$smtpd_tls_CAfile or install it in the $smtpd_tls_CApath directory. </p>

<p> RSA key and certificate examples: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_cert_file = /etc/postfix/server.pem
    smtpd_tls_key_file = $smtpd_tls_cert_file
</pre>
</blockquote>

<p> Their DSA counterparts: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_dcert_file = /etc/postfix/server-dsa.pem
    smtpd_tls_dkey_file = $smtpd_tls_dcert_file
</pre>  
</blockquote>

<p> Postfix 2.3 and later, TLS without certificates for servers serving
exclusively anonymous-cipher capable clients: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_cert_file = none
</pre>  
</blockquote>

<p> To verify a remote SMTP client certificate, the Postfix SMTP
server needs to trust the certificates of the issuing certification
authorities. These certificates in "PEM" format can be stored in a
single $smtpd_tls_CAfile or in multiple files, one CA per file in
the $smtpd_tls_CApath directory. If you use a directory, don't forget
to create the necessary "hash" links with: </p>

<blockquote>
<pre>
# <b>$OPENSSL_HOME/bin/c_rehash <i>/path/to/directory</i> </b>
</pre>
</blockquote>

<p> The $smtpd_tls_CAfile contains the CA certificates of one or
more trusted CAs. The file is opened (with root privileges) before
Postfix enters the optional chroot jail and so need not be accessible
from inside the chroot jail. </p>

<p> Additional trusted CAs can be specified via the $smtpd_tls_CApath
directory, in which case the certificates are read (with $mail_owner
privileges) from the files in the directory when the information
is needed. Thus, the $smtpd_tls_CApath directory needs to be
accessible inside the optional chroot jail. </p>

<p> When you configure the Postfix SMTP server to request <a
href="#server_vrfy_client">client certificates</a>, any CA certificates
in $smtpd_tls_CAfile are sent to the client, in order to allow it to
choose an identity signed by a CA you trust. If no $smtpd_tls_CAfile
is specified, no preferred CA list is sent, and the client is free to
choose an identity signed by any CA. Many clients use a fixed identity
regardless of the preferred CA list and you may be able to reduce TLS
negotiation overhead by installing client CA certificates mostly or
only in $smtpd_tls_CApath. In the latter case you need not specify a
$smtpd_tls_CAfile. </p>

<p> Note, that unless client certificates are used to allow greater
access to TLS authenticated clients, it is best to not ask for
client certificates at all, as in addition to increased overhead
some clients (notably in some cases qmail) are unable to complete
the TLS handshake when client certificates are requested. </p>

<p> Example: </p>
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_CAfile = /etc/postfix/CAcert.pem
    smtpd_tls_CApath = /etc/postfix/certs
</pre>
</blockquote>

<h3><a name="server_logging"> Server-side TLS activity logging </a> </h3>

<p> To get additional information about Postfix SMTP server TLS
activity you can increase the log level from 0..4. Each logging
level also includes the information that is logged at a lower
logging level. </p>

<blockquote>

<table>

<tr> <td> 0 </td> <td> Disable logging of TLS activity.</td> </tr>

<tr> <td> 1 </td> <td> Log TLS handshake and certificate information.
</td> </tr>

<tr> <td> 2 </td> <td> Log levels during TLS negotiation.  </td>
</tr>

<tr> <td> 3 </td> <td> Log hexadecimal and ASCII dump of TLS
negotiation process </td> </tr>

<tr> <td> 4 </td> <td> Log hexadecimal and ASCII dump of complete
transmission after STARTTLS </td> </tr>

</table>

</blockquote>

<p> Use log level 3 only in case of problems. Use of log level 4 is
strongly discouraged. </p>

<p> Example: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_loglevel = 0
</pre>
</blockquote>

<p> To include information about the protocol and cipher used as
well as the client and issuer CommonName into the "Received:"
message header, set the smtpd_tls_received_header variable to true.
The default is no, as the information is not necessarily authentic.
Only information recorded at the final destination is reliable,
since the headers may be changed by intermediate servers. </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_received_header = yes
</pre>
</blockquote>

<h3><a name="server_enable">Enabling TLS in the Postfix SMTP server </a> </h3>

<p> By default, TLS is disabled in the Postfix SMTP server, so no
difference to plain Postfix is visible.  Explicitly switch it on
with "smtpd_tls_security_level = may" (Postfix 2.3 and
later) or "smtpd_use_tls = yes" (obsolete but still
supported). </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    # Postfix 2.3 and later
    smtpd_tls_security_level = may
    # Obsolete, but still supported
    smtpd_use_tls = yes
</pre>
</blockquote>

<p> With this, the Postfix SMTP server announces STARTTLS support to
remote SMTP clients, but does not require that clients use TLS encryption.
</p>

<p> Note: when an unprivileged user invokes "sendmail -bs", STARTTLS
is never offered due to insufficient privileges to access the Postfix
SMTP server
private key. This is intended behavior. </p>

<p> <a name="server_enforce">You can ENFORCE the use of TLS</a>,
so that the Postfix SMTP server announces STARTTLS and accepts no
mail without TLS encryption, by setting
"smtpd_tls_security_level = encrypt" (Postfix 2.3 and
later) or "smtpd_enforce_tls = yes" (obsolete but still
supported). According to RFC 2487 this MUST NOT be applied in case
of a publicly-referenced Postfix SMTP server.  This option is off
by default and should only seldom be used. </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    # Postfix 2.3 and later
    smtpd_tls_security_level = encrypt
    # Obsolete, but still supported
    smtpd_enforce_tls = yes
</pre>
</blockquote>

<p> TLS is sometimes used in the non-standard "wrapper" mode where
a server always uses TLS, instead of announcing STARTTLS support
and waiting for remote SMTP clients to request TLS service. Some
clients, namely
Outlook [Express] prefer the "wrapper" mode.  This is true for OE
(Win32 &lt; 5.0 and Win32 &gt;=5.0 when run on a port&lt;&gt;25
and OE (5.01 Mac on all ports). </p>

<p> It is strictly discouraged to use this mode from main.cf. If
you want to support this service, enable a special port in master.cf
and specify "-o smtpd_tls_wrappermode=yes" (note: no space around
the "=") as an smtpd(8) command line option.  Port 465 (smtps) was
once chosen for this feature.
</p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/master.cf:
    smtps    inet  n       -       n       -       -       smtpd
      -o smtpd_tls_wrappermode=yes -o smtpd_sasl_auth_enable=yes
</pre>
</blockquote>

<h3><a name="server_vrfy_client">Client certificate verification</a> </h3>

<p> To receive a remote SMTP client certificate, the Postfix SMTP
server must explicitly ask for one (any contents of $smtpd_tls_CAfile
are also sent to the client as a hint for choosing a certificate from
a suitable CA). Unfortunately, Netscape clients will either complain
if no matching client certificate is available or will offer the user
client a list of certificates to choose from. Additionally some MTAs
(notably some versions of qmail) are unable to complete TLS negotiation
when client certificates are requested, and abort the SMTP session. So
this option is "off" by default. You will however need the certificate
if you want to use certificate based relaying with, for example, the
permit_tls_clientcerts feature. A server that wants client certificates
must first present its own certificate. While Postfix 2.3 by default
offers anonymous ciphers to remote SMTP clients, these are automatically
suppressed
when the Postfix SMTP server is configured to ask for client
certificates. </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_ask_ccert = yes
    # Postfix 2.3 and later
    smtpd_tls_security_level = may
    # Obsolete, but still supported
    smtpd_use_tls = yes
</pre>
</blockquote>

<p> When TLS is <a href="#server_enforce">enforced</a> you may also decide
to REQUIRE a remote SMTP client certificate for all TLS connections,
by setting "smtpd_tls_req_ccert = yes". This feature implies
"smtpd_tls_ask_ccert = yes". When TLS is not enforced,
"smtpd_tls_req_ccert = yes" is ignored and a warning is
logged. </p>

<p> Example: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_req_ccert = yes
    # Postfix 2.3 and later
    smtpd_tls_security_level = encrypt
    # Obsolete, but still supported
    smtpd_enforce_tls = yes
</pre>
</blockquote>

<p> The client certificate verification depth is specified with the
main.cf smtpd_tls_ccert_verifydepth parameter. The default verification
depth is 9 (the OpenSSL default), for compatibility with Postfix
versions before 2.5 where smtpd_tls_ccert_verifydepth was ignored.
When you configure trust in a
root CA, it is not necessary to explicitly trust intermediary CAs signed
by the root CA, unless $smtpd_tls_ccert_verifydepth is less than the
number of CAs in the certificate chain for the clients of interest. With
a verify depth of 1 you can only verify certificates directly signed
by a trusted CA, and all trusted intermediary CAs need to be configured
explicitly. With a verify depth of 2 you can verify clients signed by a
root CA or a direct intermediary CA (so long as the client is correctly
configured to supply its intermediate CA certificate). </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_ccert_verifydepth = 2
</pre>
</blockquote>

<h3><a name="server_tls_auth">Supporting AUTH over TLS only</a></h3>

<p> Sending AUTH data over an unencrypted channel poses a security
risk.  When TLS layer encryption is required
("smtpd_tls_security_level = encrypt" or the obsolete
"smtpd_enforce_tls = yes"), the Postfix SMTP server will
announce and accept AUTH only after the TLS layer has been activated
with STARTTLS. When TLS layer encryption is optional
("smtpd_tls_security_level = may" or the obsolete
"smtpd_enforce_tls = no"), it may however still be useful
to only offer AUTH when TLS is active. To maintain compatibility
with non-TLS clients, the default is to accept AUTH without encryption.
In order to change this behavior, set
"smtpd_tls_auth_only = yes". </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_auth_only = no
</pre>
</blockquote>

<h3><a name="server_tls_cache">Server-side TLS session cache</a> </h3>

<p> The Postfix SMTP server and the remote SMTP client negotiate
a session, which takes some computer time and network bandwidth.
By default, this session information is cached only in the smtpd(8)
process actually using this session and is lost when the process
terminates.  To share the session information between multiple
smtpd(8) processes, a persistent session cache can be used. You
can specify any database type that can store objects of several
kbytes and that supports the sequence operator. DBM databases are
not suitable because they can only store small objects. The cache
is maintained by the tlsmgr(8) process, so there is no problem with
concurrent access. Session caching is highly recommended, because
the cost of repeatedly negotiating TLS session keys is high.</p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
</pre>
</blockquote>

<p> Note: as of version 2.5, Postfix no longer uses root privileges     
when opening this file. The file should now be stored under the
Postfix-owned data_directory. As a migration aid, an attempt to
open the file under a non-Postfix directory is redirected to the
Postfix-owned data_directory, and a warning is logged. </p>

<p> Cached Postfix SMTP server session information expires after
a certain amount of time.  Postfix/TLS does not use the OpenSSL
default of 300s, but a longer time of 3600sec (=1 hour). RFC 2246
recommends a maximum of 24 hours.  </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_session_cache_timeout = 3600s
</pre>
</blockquote>

<p> When the Postfix SMTP server does not save TLS sessions to an
external cache database, client-side session caching is unlikely
to be useful.  To prevent such wastage, the Postfix SMTP server can
be configured to not issue TLS session ids. By default the Postfix
SMTP server always issues TLS session ids. This works around known
interoperability issues with some MUAs, and prevents possible
interoperability issues with other MTAs. </p>

<p> Example: </p>

<blockquote>
<pre>
    smtpd_tls_always_issue_session_ids = no
</pre>
</blockquote>

<h3><a name="server_access">Server access control</a> </h3>

<p> Postfix TLS support introduces three additional features for
Postfix SMTP server access control:  </p>

<blockquote>

<dl>

<dt> permit_tls_clientcerts </dt> <dd> <p> Allow the remote SMTP client
request if the client certificate fingerprint is listed in the
client certificate table (see relay_clientcerts discussion below). </p>
</dd>

<dt> permit_tls_all_clientcerts </dt> <dd> <p> Allow the remote SMTP
client request if the client certificate passes trust chain verification.
Useful with private-label CAs that only issue certificates to trusted
clients (and not otherwise). </p> </dd>

<dt> check_ccert_access type:table</dt> <dd> <p> Use the remote SMTP
client
certificate fingerprint as the lookup key for the specified access(5)
table. </p> </dd>

</dl>

</blockquote>

<p> The digest algorithm used to construct the client certificate
fingerprints is specified with the main.cf smtpd_tls_fingerprint_digest
parameter.  The default is "md5", for compatibility with Postfix
versions &lt; 2.5. </p>

<p> The permit_tls_all_clientcerts feature must be used with caution,
because it can result in too many access permissions.  Use this
feature only if a special CA issues the client certificates, and
only if this CA is listed as trusted CA. If other CAs are trusted,
any owner of a valid client certificate would be authorized.
The permit_tls_all_clientcerts feature can be practical for a
specially created email relay server.  </p>

<p> It is however recommended to stay with the permit_tls_clientcerts
feature and list all certificates via $relay_clientcerts, as
permit_tls_all_clientcerts does not permit any control when a
certificate must no longer be used (e.g. an employee leaving). </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_recipient_restrictions = 
        ... 
        permit_tls_clientcerts 
        reject_unauth_destination
        ...
</pre>
</blockquote>

<p> Example: Postfix lookup tables are in the form of (key, value)
pairs. Since we only need the key, the value can be chosen freely, e.g.
the name of the user or host:</p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    relay_clientcerts = hash:/etc/postfix/relay_clientcerts

/etc/postfix/relay_clientcerts:
    D7:04:2F:A7:0B:8C:A5:21:FA:31:77:E1:41:8A:EE:80 lutzpc.at.home
</pre>
</blockquote>

<h3><a name="server_cipher">Server-side cipher controls</a> </h3>

<p> The description below is for Postfix 2.3; for Postfix &lt; 2.3 the
smtpd_tls_cipherlist parameter specifies the acceptable ciphers as an
explicit OpenSSL cipherlist. The obsolete setting applies even when TLS
encryption is not enforced. Use of this control on public MX hosts is
strongly discouraged. </p>

<p> With mandatory TLS encryption, the Postfix SMTP server will by
default only use SSLv3 or TLSv1. SSLv2 is only used when TLS encryption
is optional. This is controlled by the smtpd_tls_mandatory_protocols
configuration parameter. </p>

<p> The Postfix SMTP server supports 5 distinct cipher security levels
as specified by the smtpd_tls_mandatory_ciphers configuration parameter,
which determines the cipher grade with mandatory TLS encryption. The
default value is "medium" which is essentially 128-bit encryption or better.
With opportunistic TLS encryption, the minimum accepted cipher grade is
always "export". </p>

<p> By default anonymous ciphers are allowed, and automatically disabled
when client certificates are requested. If clients are expected to always
verify the server certificate you may want to exclude anonymous ciphers
by setting "smtpd_tls_mandatory_exclude_ciphers = aNULL".
One can't force a client to check the server certificate, so excluding
anonymous ciphers is generally unnecessary. </p>

<p> For a server that is not a public Internet MX host, Postfix 2.3
supports configurations with no <a href="#server_cert_key">server
certificates</a> that use <b>only</b> the anonymous ciphers. This is
enabled by explicitly setting "smtpd_tls_cert_file = none"
and not specifying an smtpd_tls_dcert_file. </p>

<p> Example: (MSA that requires  TLS with high grade ciphers) </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_cert_file = /etc/postfix/cert.pem
    smtpd_tls_key_file = /etc/postfix/key.pem
    smtpd_tls_mandatory_ciphers = high
    smtpd_tls_mandatory_exclude_ciphers = aNULL, MD5
    smtpd_tls_security_level = encrypt
    smtpd_tls_mandatory_protocols = TLSv1
    # Also available with Postfix &ge; 2.5:
    smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
</pre>
</blockquote>

<p> If you want to take advantage of ciphers with ephemeral Diffie-Hellman
(EDH) key exchange (this offers "forward-secrecy"), DH parameters are
needed.  Instead of using the built-in DH parameters for both 1024-bit
(non-export ciphers) and 512-bit (export ciphers), it is better to
generate your own parameters, since otherwise it would "pay" for a
possible attacker to start a brute force attack against parameters that
are used by everybody.  Postfix defaults to compiled-in parameters
that are shared by all Postfix users who don't generate their own
settings. </p>

<p> To generate your own set of DH parameters, use: </p>

<blockquote>
<pre>
% <b>openssl gendh -out /etc/postfix/dh_512.pem -2 512</b>
% <b>openssl gendh -out /etc/postfix/dh_1024.pem -2 1024</b>
</pre>
</blockquote>

<p> Examples: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_tls_dh1024_param_file = /etc/postfix/dh_1024.pem
    smtpd_tls_dh512_param_file = /etc/postfix/dh_512.pem
</pre>
</blockquote>

<h3><a name="server_misc"> Miscellaneous server controls</a> </h3>

<p> The smtpd_starttls_timeout parameter limits the time of Postfix
SMTP server write and read operations during TLS startup and shutdown
handshake procedures.  </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtpd_starttls_timeout = 300s
</pre>
</blockquote>

<h2> <a name="client_tls">SMTP Client specific settings</a> </h2>

<p> Topics covered in this section: </p>

<ul>

<li><a href="#client_lmtp_tls"> TLS support in the LMTP delivery agent </a>

<li><a href="#client_cert_key">Client-side certificate and private
key configuration </a>

<li><a href="#client_logging"> Client-side TLS activity logging
</a>

<li><a href="#client_tls_cache">Client-side TLS session cache</a>

<li><a href="#client_tls_limits"> Client TLS limitations </a>

<li><a href="#client_tls_levels"> Client TLS security levels </a>

<li><a href="#client_tls_none"> Disabling TLS in the SMTP/LMTP client</a>

<li><a href="#client_tls_may"> Enabling TLS in the SMTP/LMTP client </a>

<li><a href="#client_tls_encrypt"> Mandating TLS encryption </a>

<li><a href="#client_tls_fprint"> Certificate fingerprint verification </a>

<li><a href="#client_tls_verify"> Mandating server certificate verification </a>

<li><a href="#client_tls_secure"> Secure server certificate verification </a>

<li><a href="#client_tls_policy"> Per-destination TLS policy </a>

<li><a href="#client_tls_obs"> Obsolete per-site TLS policy support </a>

<li><a href="#client_tls_harden"> Closing a DNS loophole with obsolete per-site TLS policies </a>

<li><a href="#client_tls_discover"> Discovering servers that support TLS </a>

<li><a href="#client_vrfy_server">Server certificate verification depth</a>

<li> <a href="#client_cipher">Client-side cipher controls </a>

<li> <a href="#client_smtps">Client-side SMTPS support </a>

<li> <a href="#client_misc"> Miscellaneous client controls </a>

</ul>

<h3><a name="client_lmtp_tls"> TLS support in the LMTP delivery agent </a>
</h3>

<p> The smtp(8) and lmtp(8) delivery agents are implemented by a
single dual-purpose program.  Specifically, all the TLS features
described below apply
equally to SMTP and LMTP, after replacing the "smtp_" prefix of the each
parameter name with "lmtp_".

<p> The Postfix LMTP delivery agent can communicate with LMTP servers
listening
on UNIX-domain sockets. When server certificate verification is enabled
and the server is listening on a UNIX-domain socket, the $myhostname
parameter is used to set the TLS verification <i>nexthop</i> and
<i>hostname</i>. Note, opportunistic encryption of LMTP traffic over
UNIX-domain sockets is futile. TLS is only useful in this context when
it is mandatory, typically to allow at least one of the server or the
client to authenticate the other. The "null" cipher grade may be
appropriate in this context, when available on both client and server.
The "null" ciphers provide authentication without encryption. </p>

<h3><a name="client_cert_key">Client-side certificate and private
key configuration </a> </h3>

<p> Do not configure Postfix SMTP client certificates unless you <b>must</b>
present
client TLS certificates to one or more servers. Client certificates are
not usually needed, and can cause problems in configurations that work
well without them. The recommended setting is to let the defaults stand: </p>

<blockquote>
<pre>
    smtp_tls_cert_file =
    smtp_tls_dcert_file =
    smtp_tls_key_file =
    smtp_tls_dkey_file =
</pre>
</blockquote>

<p> The best way to use the default settings is to comment out the above
parameters in main.cf if present. </p>

<p> During TLS startup negotiation the Postfix SMTP client may present
a certificate to the remote SMTP server.  The Netscape client is
rather clever here and lets the user select between only those
certificates that match CA certificates offered by the remote SMTP
server. As the Postfix SMTP client uses the "SSL_connect()" function
from the OpenSSL package, this is not possible and we have to choose
just one certificate.  So for now the default is to use _no_
certificate and key unless one is explicitly specified here. </p>

<p> Both RSA and DSA certificates are supported.  You can have both
at the same time, in which case the cipher used determines which
certificate is presented.  </p>

<p> It is possible for the Postfix SMTP client to use the same
key/certificate pair as the Postfix SMTP server.  If a certificate
is to be presented, it must be in "PEM" format. The private key
must not be encrypted, meaning: it must be accessible without
password. Both parts (certificate and private key) may be in the
same file. </p>

<p> In order for remote SMTP servers to verify the Postfix SMTP
client certificates, the CA certificate (in case of a certificate
chain, all CA certificates) must be available.  You should add
these certificates to the client certificate, the client certificate
first, then the issuing CA(s). </p>

<p> Example: the certificate for "client.example.com" was issued by
"intermediate CA" which itself has a certificate of "root CA".
Create the client.pem file with: </p>

<blockquote>
<pre>
% <b>cat client_cert.pem intermediate_CA.pem &gt; client.pem </b>
</pre>
</blockquote>

<p> A Postfix SMTP client certificate supplied here must be usable
as SSL client certificate and hence pass the "openssl verify -purpose
sslclient ..." test. </p>

<p> A server that trusts the root CA has a local copy of the root
CA certificate, so it is not necessary to include the root CA
certificate here. Leaving it out of the "client.pem" file reduces
the overhead of the TLS exchange. </p>

<p> If you want the Postfix SMTP client to accept remote SMTP server
certificates issued by these CAs, append the root certificate to
$smtp_tls_CAfile or install it in the $smtp_tls_CApath directory. </p>

<p> RSA key and certificate examples: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_cert_file = /etc/postfix/client.pem
    smtp_tls_key_file = $smtp_tls_cert_file
</pre>
</blockquote>

<p> Their DSA counterparts: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_dcert_file = /etc/postfix/client-dsa.pem
    smtp_tls_dkey_file = $smtp_tls_dcert_file
</pre>  
</blockquote>

<p> To verify a remote SMTP server certificate, the Postfix SMTP
client needs to trust the certificates of the issuing certification
authorities. These certificates in "pem" format can be stored in a
single $smtp_tls_CAfile or in multiple files, one CA per file in
the $smtp_tls_CApath directory. If you use a directory, don't forget
to create the necessary "hash" links with: </p>

<blockquote>
<pre>
# <b>$OPENSSL_HOME/bin/c_rehash <i>/path/to/directory</i> </b>
</pre>
</blockquote>

<p> The $smtp_tls_CAfile contains the CA certificates of one or more
trusted CAs. The file is opened (with root privileges) before Postfix
enters the optional chroot jail and so need not be accessible from inside the
chroot jail. </p>

<p> Additional trusted CAs can be specified via the $smtp_tls_CApath
directory, in which case the certificates are read (with $mail_owner
privileges) from the files in the directory when the information
is needed. Thus, the $smtp_tls_CApath directory needs to be accessible
inside the optional chroot jail.  </p>

<p> The choice between $smtp_tls_CAfile and $smtp_tls_CApath is
a space/time tradeoff. If there are many trusted CAs, the cost of
preloading them all into memory may not pay off in reduced access time
when the certificate is needed.  </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_CAfile = /etc/postfix/CAcert.pem
    smtp_tls_CApath = /etc/postfix/certs
</pre>
</blockquote>

<h3><a name="client_logging"> Client-side TLS activity logging </a> </h3>

<p> To get additional information about Postfix SMTP client TLS
activity you can increase the loglevel from 0..4. Each logging
level also includes the information that is logged at a lower
logging level. </p>

<blockquote>

<table>

<tr> <td> 0 </td> <td> Disable logging of TLS activity.</td> </tr>

<tr> <td> 1 </td> <td> Log TLS handshake and certificate information.
</td> </tr>

<tr> <td> 2 </td> <td> Log levels during TLS negotiation.  </td>
</tr>

<tr> <td> 3 </td> <td> Log hexadecimal and ASCII dump of TLS
negotiation process </td> </tr>

<tr> <td> 4 </td> <td> Log hexadecimal and ASCII dump of complete
transmission after STARTTLS </td> </tr>

</table>

</blockquote>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_loglevel = 0
</pre>
</blockquote>

<h3><a name="client_tls_cache">Client-side TLS session cache</a> </h3>

<p> The remote SMTP server and the Postfix SMTP client negotiate a
session, which takes some computer time and network bandwidth.  By
default, this session information is cached only in the smtp(8)
process actually using this session and is lost when the process
terminates.  To share the session information between multiple
smtp(8) processes, a persistent session cache can be used. You
can specify any database type that can store objects of several
kbytes and that supports the sequence operator. DBM databases are
not suitable because they can only store small objects. The cache
is maintained by the tlsmgr(8) process, so there is no problem with
concurrent access. Session caching is highly recommended, because
the cost of repeatedly negotiating TLS session keys is high.  Future
Postfix SMTP servers may limit the number of sessions that a client
is allowed to negotiate per unit time.</p>


<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_session_cache_database = btree:/var/lib/postfix/smtp_scache
</pre>
</blockquote>

<p> Note: as of version 2.5, Postfix no longer uses root privileges     
when opening this file. The file should now be stored under the
Postfix-owned data_directory. As a migration aid, an attempt to
open the file under a non-Postfix directory is redirected to the
Postfix-owned data_directory, and a warning is logged. </p>

<p> Cached Postfix SMTP client session information expires after
a certain amount of time.  Postfix/TLS does not use the OpenSSL
default of 300s, but a longer time of 3600s (=1 hour). RFC 2246
recommends a maximum of 24 hours.  </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_session_cache_timeout = 3600s
</pre>
</blockquote>

<h3><a name="client_tls_limits"> Client TLS limitations </a>
</h3>

<p> The security properties of TLS communication channels are
application specific. While the TLS protocol can provide a confidential,
tamper-resistant, mutually authenticated channel between client
and server, not all of these security features are applicable to every
communication. </p>

<p> For example, while mutual TLS authentication between browsers and web
servers is possible, it is not practical, or even useful, for web-servers
that serve the public to verify the identity of every potential user. In
practice, most HTTPS transactions are asymmetric: the browser verifies
the HTTPS server's identity, but the user remains anonymous. Much of
the security policy is up to the client. If the client chooses to not
verify the server's name, the server is not aware of this. There are many
interesting browser security topics, but we shall not dwell
on them here. Rather, our goal is to understand the security features
of TLS in conjunction with SMTP. </p>

<p> An important SMTP-specific observation is that a public MX host is
even more at the mercy of the SMTP client than is an HTTPS server. Not only
can it not enforce due care in the client's use of TLS, but it cannot even
enforce the use of TLS, because TLS support in SMTP clients is still the
exception rather than the rule. One cannot, in practice, limit access to
one's MX hosts to just TLS-enabled clients. Such a policy would result
in a vast reduction in one's ability to communicate by email with the
world at large. </p>

<p> One may be tempted to try enforcing TLS for mail from specific
sending organizations, but this, too, runs into obstacles. One such
obstacle is that we don't know who is (allegedly) sending mail until
we see the "MAIL FROM:" SMTP command, and at that point, if TLS
is not already in use, a potentially sensitive sender address (and
with SMTP PIPELINING one or more of the recipients) has (have) already been
leaked in the clear. Another obstacle is that mail from the sender to
the recipient may be forwarded, and the forwarding organization may not
have any security arrangements with the final destination. Bounces also
need to be protected. These can only be identified by the IP address and
HELO name of the connecting client, and it is difficult to keep track
of all the potential IP addresses or HELO names of the outbound email
servers of the sending organization. </p>

<p> Consequently, TLS security for mail delivery to public MX hosts is
almost entirely the client's responsibility. The server is largely a
passive enabler of TLS security, the rest is up to the client. While the
server has a greater opportunity to mandate client security policy when
it is a dedicated MSA that only handles outbound mail from trusted clients,
below we focus on the client security policy. </p>

<p> On the SMTP client, there are further complications. When delivering
mail to a given domain, in contrast to HTTPS, one rarely uses the domain
name directly as the target host of the SMTP session. More typically,
one uses MX lookups - these are usually unauthenticated - to obtain the domain's SMTP server
hostname(s). When, as is current practice, the client verifies the
insecurely obtained MX hostname, it is subject to a DNS man-in-the-middle
attack. </p>

<p> If clients instead attempted to verify the recipient domain name,
an SMTP server for multiple domains would need to
list all its email domain names in its certificate, and generate a
new certificate each time a new domain were added. At least some CAs set
fairly low limits (20 for one prominent CA) on the number of names that
server certificates can contain. This approach is not consistent with
current practice and does not scale. </p>

<p> It is regrettably the case that TLS <i>secure-channels</i>
(fully authenticated and immune to man-in-the-middle attacks) impose
constraints on the sending and receiving sites that preclude ubiquitous
deployment. One needs to manually configure this type of security for
each destination domain, and in many cases implement non-default TLS
<a href="#client_tls_policy">policy table</a> entries for additional
domains hosted at a common secured destination. With Postfix 2.3, we
make secure-channel configurations substantially easier to configure,
but they will never be the norm. For the generic domain with which you
have made no specific security arrangements, this security level is not
a good fit. </p>

<p> Given that strong authentication is not generally possible, and that
verifiable certificates cost time and money, many servers that implement
TLS use self-signed certificates or private CAs. This further limits
the applicability of verified TLS on the public Internet. </p>

<p> Historical note: while the documentation of these issues and many of the
related features are new with Postfix 2.3, the issue was well
understood before Postfix 1.0, when Lutz J&auml;nicke was designing
the first unofficial Postfix TLS patch. See his original post <a
href="http://www.imc.org/ietf-apps-tls/mail-archive/msg00304.html">http://www.imc.org/ietf-apps-tls/mail-archive/msg00304.html</a>
and the first response <a
href="http://www.imc.org/ietf-apps-tls/mail-archive/msg00305.html">http://www.imc.org/ietf-apps-tls/mail-archive/msg00305.html</a>.
The problem is not even unique to SMTP or even TLS, similar issues exist
for secure connections via aliases for HTTPS and Kerberos. SMTP merely
uses indirect naming (via MX records) more frequently. </p>

<h3><a name="client_tls_levels"> Client TLS security levels </a>
</h3>

<p> The TLS security levels listed below are described in more detail
in the sections that follow.</p>

<dl>
<dt><b>none</b></dt>
<dd><a href="#client_tls_none">No TLS.</a></dd>
<dt><b>may</b></dt>
<dd><a href="#client_tls_may">Opportunistic TLS.</a></dd>
<dt><b>encrypt</b></dt>
<dd><a href="#client_tls_encrypt">Mandatory TLS encryption.</a>
<dt><b>fingerprint</b></dt>
<dd><a href="#client_tls_fprint">Certificate fingerprint verification.</a>
<dt><b>verify</b></dt>
<dd><a href="#client_tls_verify">Mandatory server certificate verification.</a>
<dt><b>secure</b></dt>
<dd><a href="#client_tls_secure">Secure-channel TLS.</a>
</dl>

<h3><a name="client_tls_none"> Disabling TLS in the SMTP/LMTP client </a>
</h3>

<p> At the "none" TLS security level, TLS encryption is
disabled. This is the default security level. With Postfix 2.3 and later,
it can be configured explicitly by setting "smtp_tls_security_level = none". </p>

<p> With Postfix 2.2 and earlier, or when smtp_tls_security_level is set to
its default (backwards compatible) empty value, the appropriate configuration
settings are "smtp_use_tls = no" and "smtp_enforce_tls = no".
With either approach, TLS is not used even if supported by the server.
For LMTP, use the corresponding "lmtp_" parameters. </p>

<p> Per destination settings may override this default setting, in which case
TLS is used selectively, only with destinations explicitly configured
for TLS. </p>

<p> You can disable TLS for a subset of destinations, while leaving
it enabled for the rest. With the Postfix 2.3 and later TLS <a
href="#client_tls_policy">policy table</a>, specify the "none"
security level. With the obsolete <a href="#client_tls_obs">per-site</a>
table, specify the "NONE" keyword. </p>

<h3><a name="client_tls_may"> Opportunistic TLS </a>
</h3>

<p> At the "may" TLS security level, TLS encryption is <i>opportunistic</i>.
The SMTP transaction is encrypted if the STARTTLS ESMTP feature
is supported by the server. Otherwise, messages are sent in the clear.
With Postfix 2.3 and later, opportunistic TLS can be configured by
setting "smtp_tls_security_level = may".

<p> Since sending in the clear is acceptable, demanding stronger
than default TLS security merely reduces inter-operability.  For
this reason, Postfix 2.3 and later ignore the smtp_tls_mandatory_ciphers
and smtp_tls_mandatory_protocols parameters at the "may"
security level: all protocols are allowed, and "export" grade or
better ciphers are used. </p>

<p> With Postfix 2.2 and earlier, or when smtp_tls_security_level is
set to its default (backwards compatible) empty value, the appropriate
configuration settings are "smtp_use_tls = yes" and
"smtp_enforce_tls = no".
For LMTP use the corresponding "lmtp_" parameters. </p>

<p> With opportunistic TLS, mail delivery continues even if the
server certificate is untrusted or bears the wrong name.  Starting
with Postfix 2.3, when the TLS handshake fails for an opportunistic
TLS session, rather than give up on mail delivery, the transaction
is retried with TLS disabled. Trying an unencrypted connection makes
it possible to deliver mail to sites with non-interoperable server
TLS implementations. </p>

<p> Opportunistic encryption is never used for LMTP over UNIX-domain
sockets. The communications channel is already confidential without
TLS, so the only potential benefit of TLS is authentication. Do not
configure opportunistic TLS for LMTP deliveries over UNIX-domain sockets.
Only configure TLS for LMTP over UNIX-domain sockets at the
<a href="#client_tls_encrypt">encrypt</a> security level or higher.
Attempts to configure opportunistic encryption of LMTP sessions will
be ignored with a warning written to the mail logs. </p>

<p> You can enable opportunistic TLS just for selected destinations. With
the Postfix 2.3 and later TLS <a href="#client_tls_policy">policy table</a>,
specify the "may" security level. With the obsolete <a
href="#client_tls_obs">per-site</a> table, specify the "MAY" keyword.</p>

<p> This is the most common security level for TLS protected SMTP
sessions, stronger security is not generally available and, if needed,
is typically only configured on a per-destination basis. See the section
on TLS <a href="#client_tls_limits">limitations</a> above. </p>

<p> Example: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_security_level = may
</pre>
</blockquote>

<p> Postfix 2.2 syntax: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_use_tls = yes
    smtp_enforce_tls = no
</pre>
</blockquote>

<h3><a name="client_tls_encrypt"> Mandatory TLS encryption </a>
</h3>

<p> At the "encrypt" TLS security level, messages are sent only
over TLS encrypted sessions. The SMTP transaction is aborted unless
the STARTTLS ESMTP feature is supported by the remote SMTP server.
If no suitable
servers are found, the message will be deferred. With Postfix 2.3
and later, mandatory TLS encryption can be configured by setting
"smtp_tls_security_level = encrypt". Even though TLS
encryption is always used, mail delivery continues even if the server
certificate is untrusted or bears the wrong name. </p>

<p> At this security level and higher, the smtp_tls_mandatory_protocols
and smtp_tls_mandatory_ciphers configuration parameters determine
the list of sufficiently secure SSL protocol versions and the minimum
cipher strength. If the protocol or cipher requirements are not
met, the mail transaction is aborted.  The documentation for these
parameters includes useful interoperability and security guidelines.
</p>

<p> With Postfix 2.2 and earlier, or when smtp_tls_security_level
is set to its default (backwards compatible) empty value, the
appropriate configuration settings are "smtp_enforce_tls = yes"
and "smtp_tls_enforce_peername = no". For LMTP use the corresponding
"lmtp_" parameters. </p>

<p> Despite the potential for eliminating passive eavesdropping attacks,
mandatory TLS encryption is not viable as a default security level for
mail delivery to the public Internet. Most MX hosts do not support TLS at
all, and some of those that do have broken implementations. On a host
that delivers mail to the Internet, you should not configure mandatory
TLS encryption as the default security level. </p>

<p> You can enable mandatory TLS encryption just for specific destinations.
With the Postfix 2.3 and later TLS <a href="#client_tls_policy">policy
table</a>, specify the "encrypt" security level. With the
obsolete <a href="#client_tls_obs">per-site</a> table, specify the
"MUST_NOPEERMATCH" keyword. While the obsolete approach still works
with Postfix 2.3, it is strongly discouraged: users of Postfix 2.3 and later
should use the new TLS policy settings. </p>

<p> Examples: </p>

<p> In the example below, traffic to <i>example.com</i> and its sub-domains
via the corresponding MX hosts always uses TLS. The protocol version will be
"SSLv3" or "TLSv1" (the default setting of smtp_tls_mandatory_protocols
excludes "SSLv2"). Only high or medium strength (i.e. 128 bit or
better) ciphers will be used by default for all "encrypt" security
level sessions. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_policy_maps = hash:/etc/postfix/tls_policy

/etc/postfix/tls_policy:
    example.com       encrypt
    .example.com      encrypt
</pre>
</blockquote>

<p> Postfix 2.2 syntax (no support for sub-domains without resorting to
regexp tables). With Postfix 2.3 and later, do not use the obsolete <a
href="#client_tls_obs">per-site</a> table. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_per_site = hash:/etc/postfix/tls_per_site

/etc/postfix/tls_per_site:
    example.com       MUST_NOPEERMATCH
</pre>
</blockquote>

<p> In the next example, secure message submission is configured
via the MSA "<tt>[example.net]:587</tt>". TLS sessions are encrypted
without authentication, because this MSA does not possess an acceptable
certificate. This MSA is known to be capable of "TLSv1" and "high" grade
ciphers, so these are selected via the <a href="#client_tls_policy">policy
table</a>. </p>

<p><b>Note:</b> the policy table lookup key is the verbatim next-hop
specification from the recipient domain, transport(5) table or relayhost
parameter, with any enclosing square brackets and optional port. Take
care to be consistent: the suffixes ":smtp" or ":25" or no port suffix
result in different policy table lookup keys, even though they are
functionally equivalent nexthop specifications. Use at most one of these
forms for all destinations. Below, the policy table has multiple keys,
just in case the transport table entries are not specified consistently. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_policy_maps = hash:/etc/postfix/tls_policy

/etc/services:
    submission      587/tcp         msa             # mail message submission

/etc/postfix/tls_policy:
    [example.net]:587 encrypt protocols=TLSv1 ciphers=high
    [example.net]:msa encrypt protocols=TLSv1 ciphers=high
    [example.net]:submission encrypt protocols=TLSv1 ciphers=high
</pre>
</blockquote>

<p> Postfix 2.2 syntax: </p>

<p> <b>Note:</b> Avoid policy lookups with the bare hostname (for
example, "example.net").  Instead,
use the destination (for example, "[example.net]:587"), as the <a
href="#client_tls_obs">per-site</a> table lookup key (a recipient domain
or MX-enabled transport nexthop with no port suffix may look like a bare
hostname, but is still a suitable <i>destination</i>). With Postfix 2.3
and later,
do not use the obsolete <a href="#client_tls_obs">per-site</a> table;
use the new <a href="#client_tls_policy">policy table</a> instead. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_per_site = hash:/etc/postfix/tls_per_site

/etc/postfix/tls_per_site:
    [example.net]:587   MUST_NOPEERMATCH
</pre>
</blockquote>

<h3><a name="client_tls_fprint"> Certificate fingerprint verification </a>
</h3>

<p> Certificate fingerprint verification is available with Postfix 2.5 and
later. At this security level ("smtp_tls_security_level = fingerprint"),
no trusted certificate authorities are used or required.  The certificate
trust chain, expiration date, ... are not checked. Instead, the
smtp_tls_fingerprint_cert_match parameter or the "match" attribute
in the <a href="#client_tls_policy">policy</a> table lists the valid
"fingerprints" of the remote SMTP server certificate. </p>

<p> If certificate fingerprints are exchanged securely, this is the
strongest, and least scalable security level. The administrator needs to
securely collect the fingerprints of the X.509 certificates of each peer
server, store them into a local file, and update this local file
whenever the peer server's public certificate
changes. This may be feasible for an SMTP "VPN" connecting a small
number of branch offices over the Internet, or for secure connections
to a central mail hub. It works poorly if the remote SMTP server is
managed by a
third party, and its public certificate changes periodically without
prior coordination with the verifying site. </p>

<p> The digest algorithm used to calculate the fingerprint is
selected by the <b>smtp_tls_fingerprint_digest</b> parameter. In the <a
href="#client_tls_policy">policy</a> table multiple fingerprints can be
combined with a "|" delimiter in a single match attribute, or multiple
match attributes can be employed. The ":" character is not used as a
delimiter as it occurs between each pair of fingerprint (hexadecimal)
digits. </p>

<p> Example: fingerprint TLS security with an internal mailhub.
Two matching fingerprints are listed. The relayhost may be multiple
physical hosts behind a load-balancer, each with its own private/public
key and self-signed certificate. Alternatively, a single relayhost may
be in the process of switching from one set of private/public keys to
another, and both keys are trusted just prior to the transition. </p>

<blockquote>
<pre>
    relayhost = [mailhub.example.com]
    smtp_tls_security_level = fingerprint
    smtp_tls_fingerprint_digest = md5
    smtp_tls_fingerprint_cert_match =
        3D:95:34:51:24:66:33:B9:D2:40:99:C0:C1:17:0B:D1
        EC:3B:2D:B0:5B:B1:FB:6D:20:A3:9D:72:F6:8D:12:35
</pre>
</blockquote>

<p> Example: Certificate fingerprint verification with selected destinations.
As in the example above, we show two matching fingerprints: </p>
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_policy_maps = hash:/etc/postfix/tls_policy
    smtp_tls_fingerprint_digest = md5
</pre>
</blockquote>
<blockquote>
<pre>
/etc/postfix/tls_policy:
    example.com	fingerprint
        match=3D:95:34:51:24:66:33:B9:D2:40:99:C0:C1:17:0B:D1
        match=EC:3B:2D:B0:5B:B1:FB:6D:20:A3:9D:72:F6:8D:12:35
</pre>
</blockquote>

<h3><a name="client_tls_verify"> Mandatory server certificate verification </a>
</h3>

<p> At the "verify" TLS security level, messages are sent only over
TLS encrypted sessions if the remote SMTP server certificate is
valid (not
expired or revoked, and signed by a trusted certificate authority)
and where the server certificate name matches a known pattern.
Mandatory
server certificate verification can be configured by setting
"smtp_tls_security_level = verify".  The
smtp_tls_verify_cert_match parameter can override the default
"hostname" certificate name matching strategy. Fine-tuning the
matching strategy is generally only appropriate for <a
href="#client_tls_secure">secure-channel</a> destinations. </p>

<p> With Postfix 2.2 and earlier, or when smtp_tls_security_level
is set to its default (backwards compatible) empty value, the
appropriate configuration settings are "smtp_enforce_tls = yes" and
"smtp_tls_enforce_peername = yes". For LMTP use the corresponding
"lmtp_" parameters. </p>

<p> If the server certificate chain is trusted (see smtp_tls_CAfile
and smtp_tls_CApath), any DNS names in the SubjectAlternativeName
certificate extension are used to verify the remote SMTP server name.
If no
DNS names are specified, the certificate CommonName is checked.
If you want mandatory encryption without server certificate
verification, see <a href="#client_tls_encrypt">above</a>. </p>

<p> Despite the potential for eliminating "man-in-the-middle" and other
attacks, mandatory certificate trust chain and subject name verification
is not viable as a default Internet mail delivery policy.  Most MX hosts
do not support TLS at all, and a significant portion of TLS enabled
MTAs use self-signed certificates, or certificates that are signed by
a private certificate authority. On a machine that delivers mail to
the Internet, you should not configure mandatory server certificate
verification as a default policy. </p>

<p> Mandatory server certificate verification as a default security
level may be appropriate if you know that you will only connect to
servers that support RFC 2487 <i>and</i> that present verifiable
server certificates. An example would be a client that sends all
email to a central mailhub that offers the necessary STARTTLS
support. In such cases, you can often use a <a
href="#client_tls_secure">secure-channel</a> configuration instead.
</p>

<p> You can enable mandatory server certificate verification just
for specific destinations.  With the Postfix 2.3 and later TLS <a
href="#client_tls_policy">policy table</a>, specify the "verify"
security level. With the obsolete <a href="#client_tls_obs">per-site</a>
table, specify the "MUST" keyword.  While the obsolete approach
still works with Postfix 2.3, it is strongly discouraged: users of
Postfix 2.3 and later should use the new TLS policy settings. </p>

<p> Example: </p>

<p> In this example, the Postfix SMTP client encrypts all traffic to the
<i>example.com</i> domain. The peer hostname is verified, but
verification is vulnerable to DNS response forgery. Mail transmission
to <i>example.com</i> recipients uses "high" grade ciphers. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    indexed = ${default_database_type}:${config_directory}/
    smtp_tls_CAfile = ${config_directory}/CAfile.pem
    smtp_tls_policy_maps = ${indexed}tls_policy

/etc/postfix/tls_policy:
    example.com       verify ciphers=high
</pre>
</blockquote>

<p> Postfix 2.2 syntax: </p>
<blockquote>
<pre>
/etc/postfix/main.cf:
    indexed = ${default_database_type}:${config_directory}/
    smtp_tls_CAfile = ${config_directory}/CAfile.pem
    smtp_tls_per_site = ${indexed}tls_per_site

/etc/postfix/tls_per_site:
    example.com         MUST
</pre>
</blockquote>

<h3><a name="client_tls_secure"> Secure server certificate verification </a>
</h3>

<p> At the <i>secure</i> TLS security level, messages are sent only over
<i>secure-channel</i> TLS sessions where DNS forgery resistant server
certificate verification succeeds. If no suitable servers are found, the
message will be deferred. With Postfix 2.3 and later, secure-channels
can be configured by setting "smtp_tls_security_level = secure".
The smtp_tls_secure_cert_match parameter can override the default
"nexthop, dot-nexthop" certificate match strategy. </p>

<p> With Postfix 2.2 and earlier, or when smtp_tls_security_level
is set to its default (backwards compatible) empty value, the
appropriate configuration settings are "smtp_enforce_tls = yes"
and "smtp_tls_enforce_peername = yes" with additional settings to
<a href="#client_tls_harden">harden</a> peer certificate verification
against forged DNS data. For LMTP, use the corresponding "lmtp_"
parameters. </p>

<p> If the server certificate chain is trusted (see smtp_tls_CAfile and
smtp_tls_CApath), any DNS names in the SubjectAlternativeName certificate
extension are used to verify the remote SMTP server name. If no DNS names
are
specified, the CommonName is checked. If you want mandatory encryption
without server certificate verification, see <a
href="#client_tls_encrypt">above</a>. </p>

<p> Despite the potential for eliminating "man-in-the-middle" and other
attacks, mandatory secure server certificate verification is not
viable as a default Internet mail delivery policy.  Most MX hosts
do not support TLS at all, and a significant portion of TLS enabled
MTAs use self-signed certificates, or certificates that are signed
by a private certificate authority. On a machine that delivers mail
to the Internet, you should not configure secure TLS verification
as a default policy. </p>

<p> Mandatory secure server certificate verification as a default
security level may be appropriate if you know that you will only
connect to servers that support RFC 2487 <i>and</i> that present
verifiable server certificates. An example would be a client that
sends all email to a central mailhub that offers the necessary
STARTTLS support. </p>

<p> You can enable secure TLS verification just for specific destinations.
With the Postfix 2.3 and later TLS <a href="#client_tls_policy">policy table</a>,
specify the "secure" security level. With the obsolete
<a href="#client_tls_obs">per-site</a> table, specify the "MUST"
keyword and <a href="#client_tls_harden">harden</a> the certificate
verification against DNS forgery. While the obsolete approach still
works with Postfix 2.3, it is strongly discouraged: users of Postfix 2.3
and later
should use the new TLS policy settings. </p>

<p> Examples: </p>

<p> Secure-channel TLS without transport(5) table overrides: </p>

<p> The Postfix SMTP client will encrypt all traffic and verify the
destination name
immune from forged DNS responses. MX lookups are still used to find
the hostnames of the SMTP servers for <i>example.com</i>, but these
hostnames are not used when
checking the names in the server certificate(s). Rather, the requirement
is that the MX hosts for <i>example.com</i> have trusted certificates
with a subject name of <i>example.com</i> or a sub-domain, see the
documentation for the smtp_tls_secure_cert_match parameter. </p>

<p> The related domains <i>example.co.uk</i> and <i>example.co.jp</i> are
hosted on the same MX hosts as the primary <i>example.com</i> domain, and
traffic to these is secured by verifying the primary <i>example.com</i>
domain in the server certificates. This frees the server administrator
from needing the CA to sign certificates that list all the secondary
domains. The downside is that clients that want secure channels to the
secondary domains need explicit TLS <a href="#client_tls_policy">policy
table</a> entries. </p>

<p> Note, there are two ways to handle related domains.  The first is to
use the default routing for each domain, but add policy table entries
to override the expected certificate subject name.  The second is to
override the next-hop in the transport table, and use a single policy
table entry for the common nexthop.  We choose the first approach,
because it works better when domain ownership changes. With the second
approach we securely deliver mail to the wrong destination, with the
first approach, authentication fails and mail stays in the local queue,
the first approach is more appropriate in most cases. <p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_CAfile = /etc/postfix/CAfile.pem
    smtp_tls_policy_maps = hash:/etc/postfix/tls_policy

/etc/postfix/transport:

/etc/postfix/tls_policy:
    example.com     secure
    example.co.uk   secure match=example.com:.example.com
    example.co.jp   secure match=example.com:.example.com
</pre>
</blockquote>

<p> Secure-channel TLS with transport(5) table overrides: <p>

<p> In this case traffic to <i>example.com</i> and its related domains
is sent to a single logical gateway (to avoid a single point of failure,
its name may resolve to one or more load-balancer addresses, or to the
combined addresses of multiple physical hosts). All the physical hosts
reachable via the gateway's IP addresses have the logical gateway name
listed in their certificates. This secure-channel configuration can also
be implemented via a <a href="#client_tls_harden">hardened</a> variant of
the MUST policy in the obsolete <a href="#client_tls_obs">per-site</a>
table. As stated above, this approach has the potential to mis-deliver
email if the related domains change hands. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_CAfile = /etc/postfix/CAfile.pem
    transport_maps = hash:/etc/postfix/transport
    smtp_tls_policy_maps = hash:/etc/postfix/tls_policy

/etc/postfix/transport:
    example.com     smtp:[tls.example.com]
    example.co.uk   smtp:[tls.example.com]
    example.co.jp   smtp:[tls.example.com]

/etc/postfix/tls_policy:
    [tls.example.com] secure match=tls.example.com
</pre>
</blockquote>

<p> Postfix 2.2.9 and later syntax: </p>

<p> <b>Note:</b> Avoid policy lookups with the bare hostname (for
example, "tls.example.com").  Instead, use the destination (for
example, "[tls.example.com]") as the <a
href="#client_tls_obs">per-site</a> table lookup key (a recipient domain
or MX-enabled transport nexthop with no port suffix may look like a bare
hostname, but is still a suitable <i>destination</i>). With Postfix 2.3
and later,
do not use the obsolete <a href="#client_tls_obs">per-site</a> table;
use the new <a href="#client_tls_policy">policy table</a> instead. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_cname_overrides_servername = no
    smtp_tls_CAfile = /etc/postfix/CAfile.pem
    transport_maps = hash:/etc/postfix/transport
    smtp_tls_per_site = hash:/etc/postfix/tls_per_site

/etc/postfix/transport:
    example.com     smtp:[tls.example.com]
    example.co.uk   smtp:[tls.example.com]
    example.co.jp   smtp:[tls.example.com]

/etc/postfix/tls_per_site:
    [tls.example.com]       MUST
</pre>
</blockquote>

<h3> <a name="client_tls_policy"> TLS policy table </a>
</h3>

<p> Postfix 2.3 introduces a new more flexible TLS policy table. For
earlier releases, read the description of the obsolete Postfix 2.2 <a
href="#client_tls_obs">per-site</a> table. </p>

<p> A small fraction of servers offer STARTTLS but the negotiation
consistently fails. With Postfix 2.3, so long as encryption is not
enforced, the delivery is immediately retried with TLS disabled.  You no
longer need to explicitly disable TLS for the problem destinations.
As soon as their TLS software or configuration is repaired, encryption
will be used. </p>

<p> The new policy table is specified via the smtp_tls_policy_maps
parameter. This lists optional lookup tables with the Postfix SMTP client
TLS security policy by next-hop destination. When $smtp_tls_policy_maps
is not empty, the obsolete smtp_tls_per_site parameter is ignored
(a warning is written to the logs if both parameter values are
non-empty).  </p>

<p> The TLS policy table is indexed by the full next-hop destination,
which is either the recipient domain, or the verbatim next-hop
specified in the transport table, $local_transport, $virtual_transport,
$relay_transport or $default_transport. This includes any enclosing
square brackets and any non-default destination server port suffix. The
<a href="#client_lmtp_tls">LMTP</a> socket type prefix (inet: or unix:)
is not included in the lookup key. </p>

<p> Only the next-hop domain, or $myhostname with LMTP over UNIX-domain
sockets, is used as the nexthop name for certificate verification. The
port and any enclosing square brackets are used in the table lookup key,
but are not used for server name verification. </p>

<p> When the lookup key is a domain name without enclosing square brackets
or any <i>:port</i> suffix (typically the recipient domain), and the full
domain is not found in the table, just as with the transport(5) table,
the parent domain starting with a leading "." is matched recursively. This
allows one to specify a security policy for a recipient domain and all
its sub-domains. </p>

<p> The lookup result is a security level, followed by an optional
list of whitespace and/or comma separated name=value attributes
that override related main.cf settings.  The TLS security <a
href="#client_tls_levels">levels</a> are described above. Below, we
describe the corresponding table syntax: </p>

<dl>

<dt><b>none</b></dt> <dd><a href="#client_tls_none">No TLS</a>. No
additional attributes are supported at this level. </dd>

<dt><b>may</b></dt> <dd><a href="#client_tls_may">Opportunistic TLS</a>.
No additional attributes are supported at this level. </dd>

<dt><b>encrypt</b></dt> <dd><a href="#client_tls_encrypt">Mandatory
encryption</a>. Mail is delivered only if the remote SMTP
server offers STARTTLS and the TLS handshake succeeds. At this
level and higher the optional "ciphers" attribute overrides the
main.cf smtp_tls_mandatory_ciphers parameter, and the optional
"protocols" attribute
overrides the main.cf smtp_tls_mandatory_protocols parameter. </dd>

<dt><b>fingerprint</b></dt> <dd><a href="#client_tls_fprint">Certificate
fingerprint verification.</a> Available with Postfix 2.5 and
later. At this security level, there are no trusted certificate
authorities. The certificate trust chain, expiration date, ... are
not checked. Instead, the optional <b>match</b> attribute, or else
the main.cf <b>smtp_tls_fingerprint_cert_match</b> parameter,
lists the valid fingerprints of the server certificate. The
digest algorithm used to calculate fingerprints is selected by the
<b>smtp_tls_fingerprint_digest</b> parameter. Multiple fingerprints can
be combined with a "|" delimiter in a single match attribute, or multiple
match attributes can be employed. The ":" character is not used as a
delimiter as it occurs between each pair of fingerprint (hexadecimal)
digits. </dd>

<dt><b>verify</b></dt> <dd><a href="#client_tls_verify">Mandatory
server certificate verification</a>.  Mail is delivered only if the
TLS handshake
succeeds, if the remote SMTP server certificate can be validated (not
expired or revoked, and signed by a trusted certificate authority), and
if the server certificate name matches the optional "match" attribute (or
the main.cf smtp_tls_verify_cert_match parameter value when no optional
"match" attribute is specified).  </dd>

<dt><b>secure</b></dt> <dd><a href="#client_tls_secure">Secure certificate
verification.</a> Mail is delivered only if the TLS handshake succeeds,
if the remote SMTP server certificate can be validated (not expired
or revoked, and signed by a trusted certificate authority), and if the
server certificate name matches the optional "match" attribute (or the
main.cf smtp_tls_secure_cert_match parameter value when no optional
"match" attribute is specified).  </dd>

</dl>

<p> Notes: </p>

<ul>

<li> <p> The "match" attribute is especially useful to verify TLS
certificates for domains that are hosted on a shared server.  In
that case, specify "match" rules for the shared server's name.
While secure verification can also be achieved with manual routing
overrides in Postfix transport(5) tables, that approach can deliver
mail to the wrong host when domains are assigned to new gateway
hosts.  The "match" attribute approach avoids the problems of manual
routing overrides; mail is deferred if verification of a new MX
host fails.  </p>

<li> <p> When a policy table entry specifies multiple match patterns,
multiple match strategies, or multiple protocols, these must be
separated by colons.  </p>

</ul>

<p>
Example:
</p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_policy_maps = hash:/etc/postfix/tls_policy
    # Postfix 2.5 and later
    smtp_tls_fingerprint_digest = md5
/etc/postfix/tls_policy:
    example.edu             none
    example.mil             may
    example.gov             encrypt protocols=SSLv3:TLSv1 ciphers=high
    example.com             verify     
            match=hostname:dot-nexthop protocols=SSLv3:TLSv1 ciphers=high
    example.net             secure
    .example.net            secure match=.example.net:example.net
    [mail.example.org]:587  secure match=nexthop
    # Postfix 2.5 and later
    [thumb.example.org]         fingerprint
    	match=EC:3B:2D:B0:5B:B1:FB:6D:20:A3:9D:72:F6:8D:12:35
	match=3D:95:34:51:24:66:33:B9:D2:40:99:C0:C1:17:0B:D1
</pre>
</blockquote>

<p> <b>Note:</b> The "hostname" strategy if listed in a non-default setting
of smtp_tls_secure_cert_match or in the "match" attribute in the policy
table can render the "secure" level vulnerable to DNS forgery. Do not use
the "hostname" strategy for <a href="#client_tls_secure">secure-channel</a>
configurations in environments where DNS security is not assured. </p>

<h3> <a name="client_tls_obs"> Obsolete per-site TLS policy support 
</a> </h3>

<p> This section describes an obsolete per-site TLS policy mechanism.
Unlike the Postfix 2.3 <a href="#client_tls_policy">policy table</a>
mechanism, this uses as a policy lookup key a potentially untrusted
server hostname, and lacks control over what names can appear in
server certificates.  Because of this, the obsolete mechanism is
typically vulnerable to false DNS hostname information in MX or
CNAME records.  These attacks can be eliminated only with great
difficulty. The new <a href="#client_tls_policy">policy table</a>
makes <a href="#client_tls_secure">secure-channel</a> configurations
easier and provides more control over the cipher and protocol selection
for sessions with mandatory encryption. </p>

<p> Avoid policy lookups with the bare hostname.  Instead, use the
full destination nexthop (enclosed in [] with a possible ":port"
suffix) as the per-site table lookup key (a recipient domain or
MX-enabled transport nexthop with no port suffix may look like a bare
hostname, but is still a suitable <i>destination</i>).  With Postfix 2.3
and later,
use of the obsolete approach documented here is strongly discouraged:
use the new <a href="#client_tls_policy">policy table</a> instead. </p>

<p> Starting with Postfix 2.3, the underlying TLS enforcement levels are
common to the obsolete per-site table and the new policy table. The
main.cf smtp_tls_mandatory_ciphers and smtp_tls_mandatory_protocols
parameters control the TLS ciphers and protocols for mandatory
encryption regardless of which table is used. The
smtp_tls_verify_cert_match parameter determines the match strategy
for the obsolete "MUST" keyword in the same way as for the "verify"
level in the new policy. </p>

<p> With Postfix &lt; 2.3, the obsolete smtp_tls_cipherlist parameter
is also applied for opportunistic TLS sessions, and should be used with
care, or not at all. Setting cipherlist restrictions that are incompatible
with a remote SMTP server render that server unreachable, TLS handshakes
are always attempted and always fail. </p>

<p> When smtp_tls_policy_maps is empty (default) and smtp_tls_per_site
is not empty, the per-site table is searched for a policy that matches
the following information:  </p>

<blockquote>

<dl>

<dt> remote SMTP server hostname </dt> <dd> This is simply the DNS
name of the server that the Postfix SMTP client connects to; this
name may be obtained from other DNS lookups, such as MX lookups or
CNAME lookups. Use of the hostname lookup key is discouraged; always
use the next-hop destination instead. </dd>

<dt> next-hop destination </dt> <dd> This is normally the domain portion
of the recipient address, but it may be overridden by information from
the transport(5) table, from the relayhost parameter setting, or from
the relay_transport setting. When it is not the recipient domain, the
next-hop destination can have the Postfix-specific form "<tt>[name]</tt>",
"<tt>[name]:port</tt>", "<tt>name</tt>" or "<tt>name:port</tt>".  This is
the recommended lookup key for per-site policy lookups (and incidentally
for <a href="SASL_README.html#client_sasl">SASL password</a> lookups). </dd>

</dl>

</blockquote>

<p> When both the hostname lookup and the next-hop lookup succeed,
the host policy does not automatically override the next-hop policy.
Instead, precedence is given to either the more specific or the
more secure per-site policy as described below.  </p>

<p> The smtp_tls_per_site table uses a simple "<i>name whitespace
value</i>" format. Specify host names or next-hop destinations on
the left-hand side; no wildcards are allowed.  On the right hand
side specify one of the following keywords:  </p>

<blockquote>

<dl>

<dt> NONE </dt> <dd> No TLS. This overrides a less specific "MAY" lookup
result from the alternate host or next-hop lookup key, and overrides
the global smtp_use_tls, smtp_enforce_tls, and smtp_tls_enforce_peername
settings. </dd>

<dt> MAY </dt> <dd> Opportunistic TLS. This has less precedence than
a more specific result (including "NONE") from the alternate host or
next-hop lookup key, and has less precedence than the more specific global
"smtp_enforce_tls = yes" or "smtp_tls_enforce_peername = yes".  </dd>

<dt> MUST_NOPEERMATCH </dt> <dd> Mandatory TLS encryption. This
overrides a less secure "NONE" or a less specific "MAY" lookup result
from the alternate host or next-hop lookup key, and overrides the global
smtp_use_tls, smtp_enforce_tls and smtp_tls_enforce_peername settings.
</dd>

<dt> MUST </dt> <dd> Mandatory server certificate verification.
This overrides a less secure "NONE" and "MUST_NOPEERMATCH" or a
less specific "MAY" lookup result from the alternate host or next-hop
lookup key, and overrides the global smtp_use_tls, smtp_enforce_tls
and smtp_tls_enforce_peername settings.  </dd>

</dl>

</blockquote>

<p> The precedences between global (main.cf) and per-site TLS
policies can be summarized as follows: </p>

<ul>

<li> <p> When neither the remote SMTP server hostname nor the
next-hop destination are found in the smtp_tls_per_site table, the
policy is based on smtp_use_tls, smtp_enforce_tls and
smtp_tls_enforce_peername. Note: "smtp_enforce_tls = yes" and
"smtp_tls_enforce_peername = yes" imply "smtp_use_tls = yes". </p>

<li> <p> When both hostname and next-hop destination lookups produce
a result, the more specific per-site policy (NONE, MUST, etc)
overrides the less specific one (MAY), and the more secure per-site
policy (MUST, etc) overrides the less secure one (NONE).  </p>

<li> <p> After the per-site policy lookups are combined, the result
generally overrides the global policy. The exception is the less
specific "MAY" per-site policy, which is overruled by the more
specific global "smtp_enforce_tls = yes" with server certificate
verification as specified with the smtp_tls_enforce_peername
parameter.  </p>

</ul>

<h3> <a name="client_tls_harden"> Closing a DNS loophole with 
obsolete per-site TLS policies </a> </h3>

<p> For a general discussion of TLS security for SMTP see <a
href="#client_tls_limits">TLS limitations</a> above. What follows applies
only to Postfix 2.2.9 and subsequent Postfix 2.2 patch levels. Do
not use this approach with Postfix 2.3
and later; instead see the instructions under <a
href="#client_tls_secure">secure</a> server certificate verification. </p>

<p> As long as no secure DNS lookup mechanism is available, false
hostnames in MX or CNAME responses can change Postfix's notion of the
server hostname that is used for TLS policy lookup and server certificate
verification. Even with a perfect match between the server hostname and
the server certificate, there is no guarantee that Postfix is connected
to the right server.  To avoid this loophole, take all of the following
steps: </p>

<ol>

<li> <p> Use a dedicated message delivery transport (for example,
"securetls") as illustrated below. </p>

<li> <p> Eliminate MX lookups. Specify local transport(5) table
entries for sensitive domains with explicit securetls:[<i>mailhost</i>]
or securetls:[<i>mailhost</i>]:<i>port</i> destinations (you can
assure security of this table unlike DNS). This prevents false
hostname information in DNS MX records from changing Postfix's
notion of the server hostname that is used for TLS policy lookup
and server certificate verification. The "securetls" transport is
configured to enforce TLS with peername verification, and to disable
the SMTP connection cache which could interfere with enforcement
of smtp_tls_per_site policies. </p>

<li> <p> Disallow CNAME hostname overrides. In main.cf, specify
"smtp_cname_overrides_servername = no". This prevents false hostname
information in DNS CNAME records from changing the server hostname
that Postfix uses for TLS policy lookup and server certificate
verification. This feature requires Postfix 2.2.9 or later.  The
default value is "no" starting with Postfix 2.3. </p>

</ol>

<p> Example: </p>

<p> We give the <a href="postconf.5.html#default_transport">non-default</a>
"securetls" transport an explicit master.cf process limit, so that we
don't raise its process limit when raising $default_process_limit. The
total process limit for *all* transports should stay somewhat under 1024
(the typical select() file descriptor limit); otherwise transports may
be throttled under steady high load, compounding congestion.  It is not
uncommon at high volume sites to set the default process limit to 500
or more. </p>

<p> We also default the "securetls" transport TLS security level to
<a href="#client_tls_verify">MUST</a>, obviating the need for <a
href="#client_tls_obs">per-site</a> table entries for secure-channel
destinations. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    transport_maps = hash:/etc/postfix/transport

/etc/postfix/transport:
    example.com         securetls:[tls.example.com]

/etc/postfix/master.cf:
    securetls unix  -       -       n       -       100     smtp
        -o smtp_enforce_tls=yes
        -o smtp_tls_enforce_peername=yes
</pre>
</blockquote>

<h3> <a name="client_tls_discover"> Discovering servers that support
TLS </a> </h3>

<p> As we decide on a "per site" basis whether or not to use TLS,
it would be good to have a list of sites that offered "STARTTLS".
We can collect it ourselves with this option. </p>

<p> If the smtp_tls_note_starttls_offer feature is enabled and a
server offers STARTTLS while TLS is not already enabled for that
server, the Postfix SMTP client logs a line as follows: </p>

<blockquote>
<pre>
postfix/smtp[pid]: Host offered STARTTLS: [hostname.example.com]
</pre>
</blockquote>

<p> Example: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_note_starttls_offer = yes
</pre>
</blockquote>

<h3><a name="client_vrfy_server">Server certificate verification depth</a> </h3>

<p> The server certificate verification depth is specified with the
main.cf smtp_tls_scert_verifydepth parameter. The default verification
depth is 9 (the OpenSSL default), for compatibility with Postfix
versions before 2.5 where smtp_tls_scert_verifydepth was ignored.
When you configure trust
in a root CA, it is not necessary to explicitly trust intermediary CAs
signed by the root CA, unless $smtp_tls_scert_verifydepth is less than the
number of CAs in the certificate chain for the servers of interest. With
a verify depth of 1 you can only verify certificates directly signed
by a trusted CA, and all trusted intermediary CAs need to be configured
explicitly. With a verify depth of 2 you can verify servers signed by a
root CA or a direct intermediary CA (so long as the server is correctly
configured to supply its intermediate CA certificate). </p>
 
<p> Example: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_scert_verifydepth = 2
</pre>
</blockquote>

<h3> <a name="client_cipher">Client-side cipher controls </a> </h3>

<p> The Postfix SMTP client supports 5 distinct cipher security levels
as specified by the smtp_tls_mandatory_ciphers configuration
parameter. This setting controls the minimum acceptable SMTP client
TLS cipher grade for use with mandatory TLS encryption. The default
value "medium" is suitable for most destinations with which you may
want to enforce TLS, and is beyond the reach of today's crypt-analytic
methods. See smtp_tls_policy_maps for information on how to configure
ciphers on a per-destination basis. </p>

<p> By default anonymous ciphers are allowed, and automatically
disabled when remote SMTP server certificates are verified. If you
want to
disable anonymous ciphers even at the "encrypt" security level, set
"smtp_tls_mandatory_exclude_ciphers = aNULL"; and to
disable anonymous ciphers even with opportunistic TLS, set
"smtp_tls_exclude_ciphers = aNULL". There is generally
no need to take these measures. Anonymous ciphers save bandwidth
and TLS session cache space, if certificates are ignored, there is
little point in requesting them. </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_mandatory_ciphers = medium
    smtp_tls_mandatory_exclude_ciphers = RC4, MD5
    smtp_tls_exclude_ciphers = aNULL
    smtp_tls_mandatory_protocols = SSLv3, TLSv1
    # Also available with Postfix &ge; 2.5:
    smtp_tls_mandatory_protocols = !SSLv2
</pre>
</blockquote>

<h3> <a name="client_smtps">Client-side SMTPS support </a> </h3>

<p> Although the Postfix SMTP client by itself doesn't support TLS
wrapper mode, it is relatively easy to forward a connection through
the stunnel program if Postfix needs to deliver mail to some legacy
system that doesn't support STARTTLS. Use one of the following two
examples, to send only some remote mail, or to send all remote mail,
to an SMTPS server.  </p>

<h4> Sending all remote mail to an SMTPS server </h4>

<p> The first example uses SMTPS to send all remote mail to a
provider's mail server called "mail.example.com".  </p>

<p> A minimal stunnel.conf file is sufficient to set up a tunnel
from local port 11125 to the remote destination "mail.example.com"
and port "smtps". Postfix will later use this tunnel to connect to
the remote server. </p>

<blockquote>
<pre>
/path/to/stunnel.conf:
    [smtp-tls-wrapper]
    accept = 11125
    client = yes
    connect = mail.example.com:smtps
</pre>
</blockquote>

<p> To test this tunnel, use: </p>

<blockquote>
<pre>
$ telnet localhost 11125
</pre>
</blockquote>

<p> This should produce the greeting from the remote SMTP server
at mail.example.com. </p>

<p> On the Postfix side, the relayhost feature sends all remote
mail through the local stunnel listener on port 11125: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    relayhost = [127.0.0.1]:11125
</pre>
</blockquote>

<p> Use "postfix reload" to make the change effective. </p>

<h4> Sending only mail for a specific destination via SMTPS </h4>

<p> The second example will use SMTPS to send only mail for
"example.com" via SMTPS. It uses the same stunnel configuration
file as the first example, so it won't be repeated here. </p>

<p> This time, the Postfix side uses a transport map to direct only
mail for "example.com" through the tunnel: </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    transport_maps = hash:/etc/postfix/transport

/etc/postfix/transport:
    example.com  relay:[127.0.0.1]:11125
</pre>
</blockquote>

<p> Use "postmap hash:/etc/postfix/transport" and "postfix reload"
to make the change effective. </p>

<h3> <a name="client_misc"> Miscellaneous client controls </a> </h3>

<p> The smtp_starttls_timeout parameter limits the time of Postfix
SMTP client write and read operations during TLS startup and shutdown
handshake procedures.  In case of problems the Postfix SMTP client
tries the next network address on the mail exchanger list, and
defers delivery if no alternative server is available. </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_starttls_timeout = 300s
</pre>
</blockquote>

<h2><a name="tlsmgr_controls"> TLS manager specific settings </a> </h2>

<p> The security of cryptographic software such as TLS depends
critically on the ability to generate unpredictable numbers for
keys and other information. To this end, the tlsmgr(8) process
maintains a Pseudo Random Number Generator (PRNG) pool.  This is
queried by the smtp(8) and smtpd(8) processes when they initialize.
By default, these daemons request 32 bytes, the equivalent to 256
bits. This is more than sufficient to generate a 128bit (or 168bit)
session key.  </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    tls_daemon_random_bytes = 32
</pre>
</blockquote>

<p> In order to feed its in-memory PRNG pool, the tlsmgr(8) reads
entropy from an external source, both at startup and during run-time.
Specify a good entropy source, like EGD or /dev/urandom; be sure
to only use non-blocking sources (on OpenBSD, use /dev/arandom
when tlsmgr(8) complains about /dev/urandom timeout errors).
If the entropy source is not a
regular file, you must prepend the source type to the source name:
"dev:" for a device special file, or "egd:" for a source with EGD
compatible socket interface.  </p>

<p> Examples (specify only one in main.cf): </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    tls_random_source = dev:/dev/urandom
    tls_random_source = egd:/var/run/egd-pool
</pre>
</blockquote>

<p> By default, tlsmgr(8) reads 32 bytes from the external entropy
source at each seeding event.  This amount (256bits) is more than
sufficient for generating a 128bit symmetric key.  With EGD and
device entropy sources, the tlsmgr(8) limits the amount of data
read at each step to 255 bytes. If you specify a regular file as
entropy source, a larger amount of data can be read.  </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    tls_random_bytes = 32
</pre>
</blockquote>

<p> In order to update its in-memory PRNG pool, the tlsmgr(8)
queries the external entropy source again after a pseudo-random
amount of time. The time is calculated using the PRNG, and is
between 0 and the maximal time specified with tls_random_reseed_period.
The default maximal time interval is 1 hour. </p>

<p> Example: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    tls_random_reseed_period = 3600s
</pre>
</blockquote>

<p> The tlsmgr(8) process saves the PRNG state to a persistent
exchange file at regular times and when the process terminates, so
that it can recover the PRNG state the next time it starts up.
This file is created when it does not exist. </p>

<p> Examples: </p>
 
<blockquote>
<pre>
/etc/postfix/main.cf:
    tls_random_exchange_name = /var/lib/postfix/prng_exch
    tls_random_prng_update_period = 3600s
</pre>
</blockquote>

<p> As of version 2.5, Postfix no longer uses root privileges when
opening this file. The file should now be stored under the Postfix-owned
data_directory. As a migration aid, an attempt to open the file
under a non-Postfix directory is redirected to the Postfix-owned
data_directory, and a warning is logged. If you wish to continue
using a pre-existing PRNG state file, move it to the data_directory
and change the ownership to the account specified with the mail_owner
parameter.  </p>

<p> With earlier Postfix versions the default file location
is under the Postfix configuration directory, which is not the
proper place for information that is modified by Postfix.  </p>

<h2><a name="quick-start">Getting started, quick and dirty</a></h2>

<p> The following steps will get you started quickly. Because you
sign your own Postfix public key certificate, you get TLS encryption
but no TLS authentication.  This is sufficient for testing, and
for exchanging email with sites that you have no trust relationship
with.  For real authentication, your Postfix public key certificate
needs to be signed by a recognized Certificate Authority, and
Postfix needs to be configured with a list of public key certificates
of Certificate Authorities, so that Postfix can verify the public key
certificates of remote hosts. </p>

<p> In the examples below, user input is shown in <b><tt>bold</tt></b>
font, and a "<tt>#</tt>" prompt indicates a super-user shell. </p>

<ul>

<li> <p> Become your own Certificate Authority, so that you can
sign your own public keys. This example uses the CA.pl script that
ships with OpenSSL.  By default, OpenSSL installs this as
<tt>/usr/local/ssl/misc/CA.pl</tt>, but your mileage may vary. 
The script creates a private key in <tt>./demoCA/private/cakey.pem</tt>
and a public key in <tt>./demoCA/cacert.pem</tt>.</p>

<blockquote>
<pre>
% <b>/usr/local/ssl/misc/CA.pl -newca</b>
CA certificate filename (or enter to create)

Making CA certificate ...
Using configuration from /etc/ssl/openssl.cnf
Generating a 1024 bit RSA private key
....................++++++
.....++++++
writing new private key to './demoCA/private/cakey.pem'
Enter PEM pass phrase:<b>whatever</b>
</pre>
</blockquote>

<li> <p> Create an unpassworded private key for host foo.porcupine.org and create
an unsigned public key certificate. </p>

<blockquote>
<pre>
% <b>openssl req -new -nodes -keyout foo-key.pem -out foo-req.pem -days 365</b>
Using configuration from /etc/ssl/openssl.cnf
Generating a 1024 bit RSA private key
........................................++++++
....++++++
writing new private key to 'foo-key.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:<b>US</b>
State or Province Name (full name) [Some-State]:<b>New York</b>
Locality Name (eg, city) []:<b>Westchester</b>
Organization Name (eg, company) [Internet Widgits Pty Ltd]:<b>Porcupine</b>
Organizational Unit Name (eg, section) []:
Common Name (eg, YOUR name) []:<b>foo.porcupine.org</b>
Email Address []:<b>wietse@porcupine.org</b>

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:<b>whatever</b>
An optional company name []:
</pre>
</blockquote>

<li> <p> Sign the public key certificate for host foo.porcupine.org with the
Certification Authority private key that we created a few
steps ago. </p>

<blockquote>
<pre>
% <b>openssl ca -out foo-cert.pem -infiles foo-req.pem</b>
Using configuration from /etc/ssl/openssl.cnf
Enter PEM pass phrase:<b>whatever</b>
Check that the request matches the signature
Signature ok
The Subjects Distinguished Name is as follows
countryName           :PRINTABLE:'US'
stateOrProvinceName   :PRINTABLE:'New York'
localityName          :PRINTABLE:'Westchester'
organizationName      :PRINTABLE:'Porcupine'
commonName            :PRINTABLE:'foo.porcupine.org'
emailAddress          :IA5STRING:'wietse@porcupine.org'
Certificate is to be certified until Nov 21 19:40:56 2005 GMT (365 days)
Sign the certificate? [y/n]:<b>y</b>


1 out of 1 certificate requests certified, commit? [y/n]<b>y</b>
Write out database with 1 new entries
Data Base Updated
</pre>
</blockquote>

<li> <p> Install the host private key, the host public key certificate,
and the Certification Authority certificate files.  This requires
super-user privileges. </p>

<blockquote>
<pre>
# <b>cp demoCA/cacert.pem foo-key.pem foo-cert.pem /etc/postfix</b>
# <b>chmod 644 /etc/postfix/foo-cert.pem /etc/postfix/cacert.pem</b>
# <b>chmod 400 /etc/postfix/foo-key.pem</b>
</pre>
</blockquote>

<li> <p> Configure Postfix, by adding the following to
<tt>/etc/postfix/main.cf </tt>. It is generally best to not configure
client certificates, unless there are servers which authenticate your mail
submission via client certificates. Often servers that perform TLS client
authentication will issue the required certificates signed by their own
CA. If you configure the client certificate and key incorrectly, you
will be unable to send mail to sites that request client certificate,
but don't require them from all clients. </p>

<blockquote>
<pre>
/etc/postfix/main.cf:
    smtp_tls_CAfile = /etc/postfix/cacert.pem
    smtp_tls_session_cache_database =
	btree:/var/lib/postfix/smtp_tls_session_cache
    smtp_tls_security_level = may
    smtpd_tls_CAfile = /etc/postfix/cacert.pem
    smtpd_tls_cert_file = /etc/postfix/foo-cert.pem
    smtpd_tls_key_file = /etc/postfix/foo-key.pem
    smtpd_tls_received_header = yes
    smtpd_tls_session_cache_database =
	btree:/var/lib/postfix/smtpd_tls_session_cache
    tls_random_source = dev:/dev/urandom
    # Postfix 2.3 and later
    smtpd_tls_security_level = may
    # Obsolete, but still supported
    smtpd_use_tls = yes
</pre>
</blockquote>

</ul>


<h2> <a name="problems"> Reporting problems </a> </h2>

<p> Problems are preferably reported via &lt;postfix-users@postfix.org&gt;.
See http://www.postfix.org/lists.html for subscription information.
When reporting a problem, please be thorough in the report.  Patches,
when possible, are greatly appreciated too. </p>

<h2><a name="credits">Credits </a> </h2>

<ul>

<li> TLS support for Postfix was originally developed by  Lutz
J&auml;nicke at Cottbus Technical University.

<li> Wietse Venema adopted the code, did some restructuring, and
compiled this part of the documentation from Lutz's documents.

<li> Victor Duchovni was instrumental with the re-implementation
of the smtp_tls_per_site code in terms of enforcement levels, which
simplified the implementation greatly.

<li> Victor Duchovni implemented the fingerprint security level,
added more sanity checks, and separated TLS connection management
from security policy enforcement.  The latter change simplified the
code that verifies certificate signatures, certificate names, and
certificate fingerprints.

</ul>

</body>

</html>