<!-- review: finished -->

<a id="mail-ssl"></a>

# SSL

The module enables SSL/TLS encryption support for mail proxy protocols (POP3,
IMAP, SMTP), allowing secure communication between clients and the server. It
provides SSL/TLS encryption for incoming connections, supports STARTTLS
upgrades, manages certificates and keys, and controls SSL settings such as
ciphers and protocol versions.

When [building from the source code](https://en.angie.software//angie/docs/installation/sourcebuild.md#sourcebuild),
this module isn't built by default;
it should be enabled with the
`‑‑with‑mail_ssl_module`
[build option](https://en.angie.software//angie/docs/installation/sourcebuild.md#configure).

In packages and images from [our repos](https://en.angie.software//angie/docs/installation/index.md#install-packages),
the module is included in the build.

#### NOTE
This module requires the OpenSSL library.

<a id="configuration-example-54"></a>

## Configuration Example

To reduce the processor load it is recommended to

* set the number of [worker processes](https://en.angie.software//angie/docs/configuration/modules/core.md#worker-processes) equal to the number of processors,
* enable the [shared](#m-ssl-session-cache) session cache,
* disable the [built-in](#m-ssl-session-cache) session cache,
* and possibly increase the session [lifetime](#m-ssl-session-timeout) (by default, 5 minutes):

```nginx
worker_processes auto;

mail {

    ...

    server {
        listen              993 ssl;

        ssl_protocols       TLSv1.2 TLSv1.3;
        ssl_ciphers         AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
        ssl_certificate     /usr/local/angie/conf/cert.pem;
        ssl_certificate_key /usr/local/angie/conf/cert.key;
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 10m;

    #    ...
    }
```

<a id="directives-63"></a>

## Directives

<a id="index-0"></a>

<a id="m-ssl-certificate"></a>

### ssl_certificate

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_certificate` file;   |
|------------------------------------------------------------------------------------------|---------------------------|
| Default                                                                                  | —                         |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server              |

Specifies a file with the certificate in the PEM format for the given server. If intermediate certificates should be specified in addition to a primary certificate, they should be specified in the same file in the following order: the primary certificate comes first, then the intermediate certificates. A secret key in the PEM format may be placed in the same file.

This directive can be specified multiple times to load certificates of different types, for example, RSA and ECDSA:

```nginx
server {
    listen              993 ssl;

    ssl_certificate     example.com.rsa.crt;
    ssl_certificate_key example.com.rsa.key;

    ssl_certificate     example.com.ecdsa.crt;
    ssl_certificate_key example.com.ecdsa.key;

#    ...
}
```

Only OpenSSL 1.0.2 or higher supports separate certificate chains for different certificates. With older versions, only one certificate chain can be used.

The value `data:`certificate`` can be specified instead of the `file`, which loads a certificate without using intermediate files.

Note that inappropriate use of this syntax may have its security implications, such as writing secret key data to [error log](https://en.angie.software//angie/docs/configuration/modules/core.md#error-log).

<a id="index-1"></a>

<a id="m-ssl-certificate-compression"></a>

### ssl_certificate_compression

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_certificate_compression` `on` | `off`;   |
|------------------------------------------------------------------------------------------|-----------------------------------------------|
| Default                                                                                  | `ssl_certificate_compression off;`            |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                                  |

Enables TLS 1.3 [compression](https://datatracker.ietf.org/doc/html/rfc8879) of server certificates.

#### NOTE
The directive is supported when using OpenSSL 3.2 or higher; the list of supported compression algorithms is provided by the library.

#### NOTE
The directive is supported when using [BoringSSL](https://boringssl.googlesource.com/boringssl/); the list of supported compression algorithms includes `zlib`.

<a id="index-2"></a>

<a id="m-ssl-certificate-key"></a>

### ssl_certificate_key

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_certificate_key` file;   |
|------------------------------------------------------------------------------------------|-------------------------------|
| Default                                                                                  | —                             |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                  |

Specifies a file with the secret key in the PEM format for the given server.

The value `engine:`name`:id` can be specified instead of the `file`, which loads a secret key with a specified id from the OpenSSL engine name.

The value `store:scheme:id` can be specified instead of the `file`, which is used to load a secret key with a specified id and OpenSSL provider registered URI scheme, such as [pkcs11](https://datatracker.ietf.org/doc/html/rfc7512).

The value `data:`key`` can be specified instead of the `file`, which loads a secret key without using intermediate files. Note that inappropriate use of this syntax may have its security implications, such as writing secret key data to [error log](https://en.angie.software//angie/docs/configuration/modules/core.md#error-log).

<a id="index-3"></a>

<a id="m-ssl-ciphers"></a>

### ssl_ciphers

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_ciphers` ciphers;          |
|------------------------------------------------------------------------------------------|---------------------------------|
| Default                                                                                  | `ssl_ciphers HIGH:!aNULL:!MD5;` |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                    |

Specifies the enabled ciphers. The ciphers are specified in the format understood by the OpenSSL library, for example:

```nginx
ssl_ciphers ALL:!aNULL:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
```

The list of ciphers depends on the version of OpenSSL installed.
The full list can be viewed using the `openssl ciphers` command.

#### WARNING
The `ssl_ciphers` directive does *not* configure ciphers for TLS
1.3 when using OpenSSL. To tune TLS 1.3 ciphers with OpenSSL, use the
[ssl_conf_command](#m-ssl-conf-command) directive, which was added to support advanced
SSL configuration.

- In LibreSSL, TLS 1.3 ciphers *can* be configured using
  `ssl_ciphers`.
- In BoringSSL, TLS 1.3 ciphers cannot be configured at all.

<a id="index-4"></a>

<a id="m-ssl-client-certificate"></a>

### ssl_client_certificate

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_client_certificate` file;   |
|------------------------------------------------------------------------------------------|----------------------------------|
| Default                                                                                  | —                                |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                     |

Specifies a file with trusted CA certificates in the PEM format used to [verify](#m-ssl-verify-client) client certificates.

The list of certificates will be sent to clients. If this is not desired, the [ssl_trusted_certificate](#m-ssl-trusted-certificate) directive can be used.

<a id="index-5"></a>

<a id="m-ssl-conf-command"></a>

### ssl_conf_command

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_conf_command` name value;   |
|------------------------------------------------------------------------------------------|----------------------------------|
| Default                                                                                  | —                                |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                     |

Sets arbitrary OpenSSL configuration [commands](https://docs.openssl.org/master/man3/SSL_CONF_cmd/).

#### NOTE
The directive is supported when using OpenSSL 1.0.2 or higher.
To configure TLS 1.3 ciphers with OpenSSL, use the `ciphersuites` command.

Several ssl_conf_command directives can be specified on the same level:

```nginx
ssl_conf_command Options PrioritizeChaCha;
ssl_conf_command Ciphersuites TLS_CHACHA20_POLY1305_SHA256;
```

These directives are inherited from the previous configuration level if and only if there are no ssl_conf_command directives defined on the current level.

#### WARNING
Note that configuring OpenSSL directly might result in unexpected behavior.

<a id="index-6"></a>

<a id="m-ssl-crl"></a>

### ssl_crl

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_crl` file;   |
|------------------------------------------------------------------------------------------|-------------------|
| Default                                                                                  | —                 |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server      |

Specifies a file with revoked certificates (CRL) in the PEM format used to [verify](#m-ssl-verify-client) client certificates.

<a id="index-7"></a>

<a id="m-ssl-dhparam"></a>

### ssl_dhparam

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_dhparam` file;   |
|------------------------------------------------------------------------------------------|-----------------------|
| Default                                                                                  | —                     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server          |

Specifies a file with DH parameters for DHE ciphers.

#### WARNING
By default no parameters are set, and therefore DHE ciphers will not be used.

<a id="index-8"></a>

<a id="m-ssl-ecdh-curve"></a>

### ssl_ecdh_curve

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_ecdh_curve` curve;   |
|------------------------------------------------------------------------------------------|---------------------------|
| Default                                                                                  | `ssl_ecdh_curve auto;`    |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server              |

Specifies a curve for ECDHE ciphers.

#### NOTE
When using OpenSSL 1.0.2 or higher, it is possible to specify multiple curves, for example:

```nginx
ssl_ecdh_curve prime256v1:secp384r1;
```

The special value `auto` instructs Angie to use a list built into the OpenSSL library when using OpenSSL 1.0.2 or higher, or prime256v1 with older versions.

#### NOTE
When using OpenSSL 1.0.2 or higher, this directive sets the list of curves supported by the server. Thus, in order for ECDSA certificates to work, it is important to include the curves used in the certificates.

<a id="index-9"></a>

<a id="m-ssl-password-file"></a>

### ssl_password_file

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_password_file` file;   |
|------------------------------------------------------------------------------------------|-----------------------------|
| Default                                                                                  | —                           |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                |

Specifies a file with passphrases for [secret keys](#m-ssl-certificate-key) where each passphrase is specified on a separate line. Passphrases are tried in turn when loading the key.

Example:

```nginx
mail {
    ssl_password_file /etc/keys/global.pass;
    ...

    server {
        server_name mail1.example.com;
        ssl_certificate_key /etc/keys/first.key;
    }

    server {
        server_name mail2.example.com;

        # named pipe can also be used instead of a file
        ssl_password_file /etc/keys/fifo;
        ssl_certificate_key /etc/keys/second.key;
    }
}
```

<a id="index-10"></a>

<a id="m-ssl-prefer-server-ciphers"></a>

### ssl_prefer_server_ciphers

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_prefer_server_ciphers` `on` | `off`;   |
|------------------------------------------------------------------------------------------|---------------------------------------------|
| Default                                                                                  | `ssl_prefer_server_ciphers off;`            |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                                |

Specifies that server ciphers should be preferred over client ciphers when the SSLv3 and TLS protocols are used.

<a id="index-11"></a>

<a id="m-ssl-protocols"></a>

### ssl_protocols

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_protocols` [`SSLv2`] [`SSLv3`] [`TLSv1`] [`TLSv1.1`] [`TLSv1.2`] [`TLSv1.3`];   |
|------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------|
| Default                                                                                  | `ssl_protocols TLSv1.2 TLSv1.3;`                                                     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                                                                         |

#### Versionchanged
Changed in version 1.2.0: `TLSv1.3` parameter added to default set.

Enables the specified protocols.

#### NOTE
The TLSv1.1 and TLSv1.2 parameters work only when OpenSSL 1.0.1 or higher is used.

The TLSv1.3 parameter works only when OpenSSL 1.1.1 or higher is used.

<a id="index-12"></a>

<a id="m-ssl-session-cache"></a>

### ssl_session_cache

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_session_cache` `off` | `none` | [builtin[:size]] [shared:name:size];   |
|------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------|
| Default                                                                                  | `ssl_session_cache none;`                                                   |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                                                                |

Sets the types and sizes of caches that store session parameters. A cache can be of any of the following types:

| `off`     | the use of a session cache is strictly prohibited: Angie explicitly tells a client that sessions may not be reused.                                                                                                                                                                                                                                                                                                                                |
|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `none`    | the use of a session cache is gently disallowed: Angie tells a client that sessions may be reused, but does not actually store session parameters in the cache.                                                                                                                                                                                                                                                                                    |
| `builtin` | a cache built in OpenSSL; used by one worker process only. The cache size is specified in sessions. If size is not given, it is equal to 20480 sessions. Use of the built-in cache can cause memory fragmentation.                                                                                                                                                                                                                                 |
| `shared`  | a cache shared between all worker processes. The cache size is specified in bytes; one megabyte can store about 4000 sessions. Each shared cache should have an arbitrary name. A cache with the same name can be used in several servers. It is also used to automatically generate, store, and periodically rotate TLS session ticket keys unless configured explicitly using the [ssl_session_ticket_key](#m-ssl-session-ticket-key) directive. |

Both cache types can be used simultaneously, for example:

```nginx
ssl_session_cache builtin:1000 shared:SSL:10m;
```

but using only shared cache without the built-in cache should be more efficient.

<a id="index-13"></a>

<a id="m-ssl-session-ticket-key"></a>

### ssl_session_ticket_key

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_session_ticket_key` file;   |
|------------------------------------------------------------------------------------------|----------------------------------|
| Default                                                                                  | —                                |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                     |

Sets a file with the secret key used to encrypt and decrypt TLS session tickets. The directive is necessary if the same key has to be shared between multiple servers. By default, a randomly generated key is used.

If several keys are specified, only the first key is used to encrypt TLS session tickets. This allows configuring key rotation, for example:

```nginx
ssl_session_ticket_key current.key;
ssl_session_ticket_key previous.key;
```

The file must contain 80 or 48 bytes of random data and can be created using the following command:

```console
openssl rand 80 > ticket.key
```

Depending on the file size either AES256 (for 80-byte keys) or AES128 (for 48-byte keys) is used for encryption.

<a id="index-14"></a>

<a id="m-ssl-session-tickets"></a>

### ssl_session_tickets

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_session_tickets` `on` | `off`;   |
|------------------------------------------------------------------------------------------|---------------------------------------|
| Default                                                                                  | `ssl_session_tickets on;`             |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                          |

Enables or disables session resumption through [TLS session tickets](https://datatracker.ietf.org/doc/html/rfc5077).

<a id="index-15"></a>

<a id="m-ssl-session-timeout"></a>

### ssl_session_timeout

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_session_timeout` time;   |
|------------------------------------------------------------------------------------------|-------------------------------|
| Default                                                                                  | `ssl_session_timeout 5m;`     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                  |

Specifies a time during which a client may reuse the session parameters.

<a id="index-16"></a>

<a id="m-ssl-trusted-certificate"></a>

### ssl_trusted_certificate

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_trusted_certificate` file;   |
|------------------------------------------------------------------------------------------|-----------------------------------|
| Default                                                                                  | —                                 |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                      |

Specifies a file with trusted CA certificates in the PEM format used to [verify](#m-ssl-verify-client) client certificates.

In contrast to the certificate set by [ssl_client_certificate](#m-ssl-client-certificate), the list of these certificates will not be sent to clients.

<a id="index-17"></a>

<a id="m-ssl-verify-client"></a>

### ssl_verify_client

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_verify_client` `on` | `off` | `optional` | `optional_no_ca`;   |
|------------------------------------------------------------------------------------------|---------------------------------------------------------------------|
| Default                                                                                  | `ssl_verify_client off;`                                            |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                                                        |

Enables verification of client certificates. The verification result is passed in the `Auth-SSL-Verify` header of the [authentication](https://en.angie.software//angie/docs/configuration/modules/mail/mail_auth_http.md#m-auth-http) request. If an error occurs during client certificate verification or a client does not provide the required certificate, the connection is closed.

| `optional`       | requests the client certificate and verifies it if the certificate is present                                                                                                                                                                                                                                                                                                                                                                    |
|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `optional_no_ca` | requests the client certificate but does not require it to be signed by a trusted CA certificate. This is intended for use in cases when a service that is external to Angie performs the actual certificate verification. The contents of the certificate are accessible through requests [sent](https://en.angie.software//angie/docs/configuration/modules/mail/mail_auth_http.md#m-auth-http-pass-client-cert) to the authentication server. |

<a id="index-18"></a>

<a id="m-ssl-verify-depth"></a>

### ssl_verify_depth

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `ssl_verify_depth` number;   |
|------------------------------------------------------------------------------------------|------------------------------|
| Default                                                                                  | `ssl_verify_depth 1;`        |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                 |

Sets the verification depth in the client certificates chain.

<a id="index-19"></a>

<a id="m-starttls"></a>

### starttls

| [Syntax](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)   | `starttls` `on` | `off` | `only`;   |
|------------------------------------------------------------------------------------------|-------------------------------------|
| Default                                                                                  | `starttls off;`                     |
| [Context](https://en.angie.software//angie/docs/configuration/configfile.md#configfile)  | mail, server                        |

| `on`   | allow usage of the STLS command for the POP3 and the STARTTLS command for the IMAP and SMTP;   |
|--------|------------------------------------------------------------------------------------------------|
| `off`  | deny usage of the STLS and STARTTLS commands;                                                  |
| `only` | require preliminary TLS transition.                                                            |
