Sub topic 10
TLS-based VPNs
This chapter assumes you know what encapsulation is, how a routing table and default route work, and the difference between TCP and UDP. It also assumes you have read the earlier chapters on key exchange, public keys and certificates. You should be comfortable with a Linux command line.
What a TLS-based VPN is
A TLS-based VPN uses the Transport Layer Security (TLS) protocol to authenticate the two ends and to agree on session keys. TLS is the protocol behind HTTPS. After the handshake, the VPN software carries the tunnelled IP packets (or Ethernet frames) as encrypted data between the two peers.
The best-known example is OpenVPN. Many commercial "SSL VPN" products work on the same idea, although they differ in details.
The main point is that a TLS-based VPN runs in user space, as an ordinary program, rather than inside the kernel's IP stack. IPsec, in contrast, is built into the operating system's network stack. A TLS-based VPN uses a virtual network device (a TUN or TAP device) to hand packets to the program.
- A TUN device is a virtual layer 3 interface. The program receives and sends IP packets.
- A TAP device is a virtual layer 2 interface. The program receives and sends Ethernet frames, so it can bridge broadcast traffic.
Control channel and data channel
OpenVPN splits its traffic into two logical parts that share one network connection.
- The control channel carries the TLS handshake and later key renegotiation. It needs reliable, ordered delivery.
- The data channel carries the tunnelled packets, encrypted with symmetric keys derived from the handshake.
Over UDP, OpenVPN adds its own small reliability layer for the control channel. Over TCP, the transport already provides reliability.
Authentication in the handshake
The TLS handshake authenticates the peers with certificates. A typical OpenVPN setup uses a private certificate authority (CA) that you run yourself.
- The server has a certificate signed by the CA.
- Each client has its own certificate, also signed by the CA.
- The server checks the client certificate against the CA. It can also check a revocation list, so a lost laptop can be locked out.
Many deployments add a second factor. The server may also require a username and password, or a one-time code, checked by a plugin against a directory service. Certificates prove which device is connecting. The password or code proves which person is behind it.
A TLS handshake also gives forward secrecy when it uses ephemeral Diffie-Hellman key exchange. Each session gets fresh keys, so a stolen long-term private key cannot decrypt old recorded traffic.
Transport choices: UDP or TCP
A TLS-based VPN can run over either UDP or TCP. This is the most important practical choice you make.
| Transport | Strength | Weakness |
|---|---|---|
| UDP | Low overhead. Lost packets are not retransmitted twice, so inner TCP handles loss itself. | Some firewalls block unusual UDP ports. |
| TCP | Passes through most firewalls, especially on port 443. Works through HTTP proxies. | Suffers from "TCP over TCP" problems. |
The TCP-over-TCP problem needs explanation. If you tunnel a TCP connection inside another TCP connection, both layers detect loss and both retransmit. When a packet is lost, the outer connection stalls to repair it. Meanwhile the inner connection's timer expires and it retransmits too, and it also slows down. The two retransmission mechanisms interfere, and throughput can collapse on a lossy link. For this reason, use UDP when you can. Use TCP when a restrictive network leaves you no other option.
Running over TCP port 443 often lets the VPN pass firewalls that only permit web traffic. This makes TLS-based VPNs popular for travellers on hotel and airport networks. Be aware that some deep-inspection firewalls can still recognise the protocol and block it.
Layering and overhead
Each tunnelled packet gains several headers. With OpenVPN over UDP, the packet carries the outer IP header, the outer UDP header, the OpenVPN header, and the encryption overhead (an IV or nonce and an authentication tag). Over TCP, the TCP header is larger than UDP's and adds to the cost.
This overhead reduces the usable MTU of the tunnel, as covered in the earlier chapter on MTU. If you leave the virtual interface at a default of 1500, large packets will not fit in the physical link and will be fragmented or dropped. OpenVPN has options such as tun-mtu and mssfix to deal with this. The mssfix option rewrites the TCP maximum segment size in tunnelled TCP handshakes so that endpoints send segments that fit.
A minimal OpenVPN configuration
This is a client configuration for a routed (TUN) tunnel over UDP. The file names are placeholders for files you create with your own CA.
client
dev tun
proto udp
remote vpn.example.com 1194
resolv-retry infinite
nobind
persist-key
persist-tun
ca ca.crt
cert client.crt
key client.key
remote-cert-tls server
cipher AES-256-GCM
verb 3The line remote-cert-tls server is important. It makes the client check that the server's certificate is marked for server use. Without it, any other certificate signed by your CA, such as a client certificate, could be used to impersonate the server in a man-in-the-middle attack.
You start the client with the command below. It reads the file and creates a tun0 interface.
sudo openvpn --config client.ovpnOnce connected, check the new interface and the routes the server pushed.
ip addr show tun0
ip routePushed routes and DNS
A TLS-based VPN server often pushes settings to the client after the handshake. These can include an inner IP address, routes and DNS servers. Which routes the server pushes decides whether the tunnel is split or full, a topic covered in the chapter on routing policy. For example, a server directive push "redirect-gateway def1" sends all traffic through the tunnel. A directive such as push "route 10.20.0.0 255.255.0.0" sends only that subnet.
Pushed DNS servers matter too. If the client keeps using its local resolver, internal names will not resolve, and DNS queries can leak outside the tunnel.
Comparison with other approaches
| Property | TLS-based VPN (OpenVPN) | IPsec | WireGuard |
|---|---|---|---|
| Runs in | User space, with a TUN or TAP device | Kernel | Kernel on Linux |
| Transport | UDP or TCP | ESP (IP protocol 50), or UDP 4500 with NAT traversal | UDP |
| Authentication | Certificates, plus optional passwords | Certificates or pre-shared keys through IKE | Static public keys |
| Firewall traversal | Strong, especially TCP 443 | Needs ESP or UDP 500/4500 | Needs a UDP port |
| Configuration size | Moderate | Large | Small |
TLS-based VPNs trade some raw speed for flexibility. Packet processing in user space involves extra copies between the kernel and the program, so throughput is usually lower than a kernel implementation. In return you get easy deployment through restrictive networks and rich authentication options.
Common errors and edge cases
- Certificate expired. The handshake fails with a verification error. Check the clock on both machines first, because a wrong system time makes valid certificates look expired or not yet valid.
- Client and server cipher mismatch. Both sides must share at least one supported cipher. Modern versions negotiate this, but old configurations that fix a cipher on one side can fail.
- Wrong transport or port. A client using UDP cannot reach a server listening on TCP. The connection simply times out.
- Large transfers stall while small pings work. This is the classic MTU black hole. Lower the tunnel MTU or use
mssfix. - Names do not resolve. The server did not push DNS settings, or the client ignored them. Check the resolver configuration after connecting.
- TCP stalls on a lossy link. You are probably seeing TCP over TCP. Switch to UDP if the network allows it.
- Impersonated server. A missing
remote-cert-tls servercheck, or missing CA verification, lets an attacker present a valid-looking certificate.
Choosing a TLS-based VPN
A TLS-based VPN is a good fit when:
- Clients must connect from networks that block everything except web traffic.
- You want to reuse an existing certificate authority or user directory.
- You need per-user authentication with passwords or one-time codes.
- You need layer 2 bridging, which TAP devices support.
It is a weaker fit when raw throughput on a high-speed link is the main requirement, or when you want the smallest possible code and configuration.
Exercise
A traveller on a hotel network can connect to a TLS-based VPN over UDP port 1194 only on some days. On other networks, the port is blocked. You switch the server to TCP port 443. The connection now works everywhere, but file transfers over the VPN are slow whenever the Wi-Fi is poor. Explain why, and say what you would do to get the best result.
Show answer
The TCP transport lets the VPN pass firewalls that only allow web traffic. However, TCP carried inside TCP means both the outer VPN connection and the inner file transfer retransmit lost data. The outer connection stalls while it repairs the loss, the inner connection's timer fires and it retransmits and slows down too, so throughput collapses on a lossy link. The best result is to offer both listeners: UDP as the default, and TCP 443 as a fallback. The client should try UDP first and use TCP only on networks that block UDP.
Exercise
Your client configuration omits remote-cert-tls server. Both server and client certificates are signed by your private CA. What attack becomes possible, and how does the option prevent it?
Show answer
Someone holding a legitimate client certificate from your CA could set up a rogue server and present that certificate to a victim. The victim's client would accept it, because it is signed by the trusted CA, so the attacker could intercept the traffic in a man-in-the-middle attack. The option makes the client require that the server certificate be marked for server use, so a client certificate is rejected.