# TLS Module

## Admin Guide

### Overview

This module implements the TLS transport for &kamailio; using the &openssl; library. To enable the &kamailio; TLS support this module must be loaded and enable_tls=yes core setting must be added to the Kamailio config file.

Read the "Important Notes" section because it has relevant information about configuring properly the module for various libssl versions, components and operating systems.

### Quick Start

The default &kamailioconfig; file has basic tls support included, it has to be enabled with "#!define WITH_TLS" directive.

The most important parameters to set the path to the public certificate and private key files. You can either have them in different file or in the same file in PEM format. The parameters for them are `certificate` and `private_key`. They can be given as modparam or provided in the profiles of tls.cfg file.

When installing tls module of kamailio, a sample 'tls.cfg' file is deployed in the same folder with 'kamailio.cfg'. For freshly generated self signed certificates make must be called from tls folder

    make install-tls-cert

HINT: be sure you have enable_tls=yes to your kamailio.cfg.

#### Quick Start Basic Config

    #...
    loadmodule "sl.so"
    loadmodule "tls.so"

    modparam("tls", "private_key", "./server-test.pem")
    modparam("tls", "certificate", "./server-test.pem")
    modparam("tls", "ca_list", "./calist.pem")

    enable_tls=yes

    request_route {
    	if(proto != TLS) {
    		sl_send_reply("403", "Accepting TLS Only");
    		exit;
    	}
    	...
    }

### Important Notes

The TLS module needs some special options enabled when compiling Kamailio. These options are enabled by default, however in case you are using a modified Kamailio version or Makefile, make sure that you enable -DUSE_TLS and -DTLS_HOOKS (or compile with make TLS_HOOKS=1 which will take care of both options).

To quickly check if your Kamailio version was compiled with these options, run kamailio -V and look for USE_TLS and TLS_HOOKS among the flags.

This module includes several workarounds for various Openssl bugs (like compression and Kerberos using the wrong memory allocations functions, low memory problems a.s.o). On startup it will try to enable the needed workarounds based on the OpenSSL library version. Each time a known problem is detected and a workaround is enabled, a message will be logged. In general it is recommended to compile this module on the same machine or a similar machine to where kamailio will be run or to link it statically with libssl. For example if on the compile machine OpenSSL does not have the Kerberos support enabled, but on the target machine a Kerberos enabled OpenSSL library is installed, Kamailio cannot apply the needed workarounds and will refuse to start. The same thing will happen if the OpenSSL versions are too different (to force Kamailio startup anyway, see the `tls_force_run` module parameter).

Compression is fully supported if you have a new enough OpenSSL version (starting with 0.9.8). Although there are some problems with zlib compression in currently deployed OpenSSL versions (up to and including 0.9.8d, see openssl bug #1468), the TLS module will automatically switch to its own fixed version. Note however that starting with &kamailio; 3.1 compression is not enabled by default, due to the huge extra memory consumption that it causes (about 10x more memory). To enable it use modparam("tls", "tls_disable_compression", 0) (see `tls_disable_compression`).

The TLS module includes workarounds for the following known openssl bugs:

- openssl #1204 (disable SS_OP_TLS_BLOCK_PADDING_BUG if compression is enabled, for versions between 0.9.8 and 0.9.8c),

- openssl #1468 (fix zlib compression memory allocation),

- openssl #1467 (kerberos support will be disabled if the openssl version is less than 0.9.8e-beta1)

- openssl #1491 (stop using tls in low memory situations due to the very high risk of openssl crashing or leaking memory).

The bug reports can be viewed at http://rt.openssl.org/.

Note 1: the tls module must be loaded before any other &kamailio; module that uses libssl (OpenSSL library). A safe option is to have the tls module loaded first (be in the first "loadmodule" in &kamailioconfig;).

Note 2: for libssl v3.x, the core parameter "tls_threads_mode" has to be set, see the Core Cookbook for possible values.

Note 3: using this module compiled with newer versions of libssl (e.g., v1.1+) may require &kamailio; to be started with --atexit=no command line parameters to avoid calling C atexit callbacks inside the process ending during daemonize procedure as well as during shut down, which can lead to crashes because it destroys and then accesses shared memory. For example, such case has been reported for Ubuntu 20.04 or RedHat 8.

Note 4: with some particular combination of OS, openssl and mysql-client libraries, there were reports of random crashes, in such case try to set the db_mysql module parameter opt_ssl_mode to 1.

### Compiling the TLS Module

In most case compiling the TLS module is as simple as:

    make -C modules/tls

or

    make modules modules=modules/tls

or (compiling whole Kamailio and the tls module)

    make all include_modules=tls

.

However in some cases the OpenSSL library requires linking with other libraries. For example compiling the OpenSSL library with Kerberos and zlib-shared support will require linking the TLS module with libkrb5 and libz. In this case just add TLS_EXTRA_LIBS="library list" to make's command line. E.g.:

    make TLS_EXTRA_LIBS="-lkrb5 -lz" all include_modules=tls

In general, if Kamailio fails to start with a symbol not found error when trying to load the TLS module (check the log), it means some needed library was not linked and it must be added to TLS_EXTRA_LIBS

Elliptic Curve Diffie-Hellman (EDCH)-Ciphers are only supported in OpenSSL 1.0.0e and later.

### TLS and Low Memory

The Openssl library doesn't handle low memory situations very well. If memory allocations start to fail (due to memory shortage), Openssl can crash or cause memory leaks (making the memory shortage even worse). As of this writing all Openssl versions were affected (including 0.9.8e), see Openssl bug #1491. The TLS module has some workarounds for preventing this problem (see `low_mem_threshold1` and `low_mem_threshold2`), however starting Kamailio with enough shared memory is higly recommended. When this is not possible a quick way to significantly reduce Openssl memory usage it to disable compression (see `tls_disable_compression`).

### TLS Debugging

Debugging messages can be selectively enabled by recompiling the TLS module with a combination of the following defines:

- TLS_WR_DEBUG - debug messages for the write/send part.

- TLS_RD_DEBUG - debug messages for the read/receive part.

- TLS_BIO_DEBUG - debug messages for the custom BIO.

#### Compiling TLS with Debug Messages

    make -C modules/tls extra_defs="-DTLS_WR_DEBUG -DTLS_RD_DEBUG"

To change the level at which the debug messages are logged, change the `tls_debug` module parameter.

### Known Limitations

The private key must not be encrypted (Kamailio cannot ask you for a password on startup).

The TLS certificate verifications ignores the certificate name, Subject Altname and IP extensions, it just checks if the certificate is signed by a recognized CA. One can use the select framework to try to overcome this limitation (check in the script for the contents of various certificate fields), but this is not only slow, but also not exactly standard conforming (the verification should happen during TLS connection establishment and not after).

This documentation is incomplete. The provided selects are not documented in this file. A list with all the ones implemented by the TLS module can be found in the Cookbook &kamwiki; in the section Selects for the respective version of &kamailio;.

### Event Routes

#### event_route[tls:connection-out]

Event route to be executed when a TLS connection is opened by &kamailio;. If drop() is executed in the event route, then the data is no longer sent over the connection.

##### Use of `event_route[tls:connection-out]`

    ...
    event_route[tls:connection-out] {
      if($sndto(ip)=="1.2.3.4") {
        drop;
      }
    }
    ...

### TLS With Database Backend

The module does not connect to database to fetch the values for the TLS profile attributes. However the 'kamcli' tool can generate the tls.cfg from a database table. Once generated, the 'tls.cfg' can be reloaded with an RPC command.

The kamcli tool can be found at https://github.com/kamailio/kamcli.

The schema to create the database table can be seen with the command: "kamcli tls sqlprint". The default name for database table is 'tlscfg'.

The most of the column names matches the corresponding attribute names from a TLS profile.

The profile id in 'tls.cfg' is generated from '[profile_type:profile_name]'. The 'profile_type' has to be 'server' or 'client'. The 'profile_name' can be 'default', 'any' or the pair of IP address and port like 'ipaddr:port'.

The 'file_type' is specifying if the values for 'certificate', 'private_key', 'ca_list' and 'crl' are path to files on disc (when is set to 0) or the content of the files (when set to 1). If 'file_type' is 1, then 'kamcli' will create new files on disc and store the values from the database in them. The target folder for 'tls.cfg' and the certificates related files can be set via command options for 'kamcli tls', for more details see the output of 'kamcli tls --help' and 'kamcli tls cfgprint --help'.