stunnel: Authentication

Either the TLS client, the TLS server, or both need to be authenticated:

  • Server authentication prevents Man-In-The-Middle (MITM) attacks on the encryption protocol.
  • Client authentication allows for restricting access for individual clients (access control).


The easiest way to configure authentication is with PSK (Pre-Shared Key). It provides both client and server authentication. PSK is also the fastest TLS authentication.


PSK authentication requires stunnel version 5.09 or higher.

Server Configuration

A trivial configuration example:

[PSK server]
accept = <server_port>
connect = <dst_port>
ciphers = PSK
PSKsecrets = psk.txt

The psk.txt file contains one line for each client:


Client Configuration

A trivial configuration example:

[PSK client 1]
client = yes
accept =<src_port>
connect = <server_host>:<server_port>
PSKsecrets = psk1.txt

The psk1.txt file only needs a single line:


Each client needs a separate secret. Otherwise, all the clients sharing the same key will have to be reconfigured if the key is compromised.


For simplicity, this tutorial only covers server authentication. The advantage of this configuration is that it does not require individual secrets for each of the clients.

Server Configuration

Unless PSK authentication is configured, each stunnel server needs a certificate with the corresponding private key. The Windows installer of stunnel automatically builds a certificate. On Unix platforms, a certificate can be built with "make cert". A certificate can also be purchased from one of the available commercial certificate authorities.

A trivial configuration example:

[certificate-based server]
accept = <server_port>
connect = <dst_port>
cert = cert.pem
key = key.pem

The "key" option may be omitted if cert.pem also contains the private key.

Client Configuration

stunnel can use an existing PKI (Public Key Infrastructure). The following configuration requires stunnel 5.15 or later:

[PKI client]
client = yes
accept =<src_port>
connect = <server_host>:<server_port>
verifyChain = yes
CAfile = ca-certs.pem
checkHost = <server_host>

The ca-certs.pem file contains the certificates of trusted certificate authorities.

Alternatively, a technique known as certificate pinning can be used. The following configuration requires stunnel version 4.46 or higher:

[pinning client]
client = yes
accept =<src_port>
connect = <server_host>:<server_port>
verifyPeer = yes
CAfile = peer-certificate.pem

The peer-certificate.pem file needs to contain the server certificate.

0 (0)
Article Rating (No Votes)
Rate this article
There are no attachments for this article.
There are no comments for this article. Be the first to post a comment.
Full Name
Email Address
Security Code Security Code
Related Articles RSS Feed
Using renice and taskset to manage process priority and CPU affinity with Linux OEL 6.4
Viewed 1579 times since Mon, Feb 17, 2020
Linux - How to perform I/O performance test with dd command
Viewed 4706 times since Fri, Jun 8, 2018
HOWTO: Use SSL/port 465 in smarthost stunnel
Viewed 1680 times since Fri, Sep 28, 2018
15 Linux Yum Command Examples – Install, Uninstall, Update Packages
Viewed 1762 times since Thu, Oct 25, 2018
Installing and Configuring an OCFS2 Clustered File System
Viewed 4341 times since Sat, Jun 2, 2018
Linux 16 Useful Bandwidth Monitoring Tools to Analyze Network Usage in Linux
Viewed 7617 times since Mon, Sep 21, 2020
Viewed 15318 times since Sat, Jun 2, 2018
Przekazywanie portów TCP rinetd
Viewed 40065 times since Thu, May 24, 2018
Super Grub2 Disk
Viewed 1465 times since Wed, May 22, 2019
Stunnel Setup
Viewed 12952 times since Fri, Sep 28, 2018