SkillPilotAI

openvpn (2/3)

Part 2 of OpenVPN covers what decides security and connectivity: SSL library info, key generation, TLS mode certificate checks, PKCS#11, data-channel renegotiation, link options (keepalive, ports, MTU) and the tun adapter with addressing and routing.

117.1 Lab setup

  • Docker on Alpine 3.20 with OpenVPN 2.6.20, network 192.168.137.0/24: VPN server 192.168.137.10, file server 192.168.137.150, laptop1 192.168.137.20.
  • Same option names on Windows (openvpn.exe) and macOS; no DCO in the kernel, so --disable-dco.

117.2 How the tunnel is built

TLS handshake, PUSH_REPLY and data
  1. 1.laptop1 sends TLS ClientHello on UDP 1194

  2. 2.Server verifies the laptop1 cert with its CA: VERIFY OK CN=laptop1

  3. 3.Laptop checks the server cert name: VERIFY X509NAME OK CN=vpn-server

  4. 4.PUSH_REPLY: tun0 10.8.0.2/24, route, mtu 1400

  5. 5.Ping to 192.168.137.150 goes as ICMP inside UDP 1194, reply ttl=63

  6. 6.Data key renegotiated (lab: every 40 packets)

117.3 SSL library information (5)

Terminal
openvpn --show-ciphers | grep GCMopenvpn --show-ciphers | grep -c bitopenvpn --show-digests | grep -E '^SHA(256|384|512) 'openvpn --show-tls | head -12openvpn --show-groups | grep -E '^(secp384r1|prime256v1)'openvpn --show-engines
Output
AES-128-GCM  (128 bit key, 128 bit block, ...)AES-192-GCM  (192 bit key, 128 bit block, ...)AES-256-GCM  (256 bit key, 128 bit block, ...)ARIA-128-GCM  (128 bit key, ...)65SHA512 512 bit digest sizeSHA384 384 bit digest sizeSHA256 256 bit digest sizeAvailable TLS Ciphers, listed in order of preference:For TLS 1.3 and newer (--tls-ciphersuites):TLS_AES_256_GCM_SHA384TLS_CHACHA20_POLY1305_SHA256TLS_AES_128_GCM_SHA256For TLS 1.2 and older (--tls-cipher):TLS-ECDHE-ECDSA-WITH-AES-256-GCM-SHA384TLS-ECDHE-RSA-WITH-AES-256-GCM-SHA384secp384r1prime256v1Sorry, OpenSSL hardware crypto enginefunctionality is not available.
  • --show-ciphers = data-channel ciphers (65 here), --show-digests = HMAC hashes, --show-tls = control-channel suites in preference order, --show-groups = ECDH curves.

117.4 Generating key material (8)

Terminal
cd /keysopenvpn --genkey secret | head -4openvpn --genkey secret static.keyopenvpn --genkey tls-auth ta.keyopenvpn --genkey tls-crypt tc.keyopenvpn --genkey auth-token token.keyls -l | awk '{print $5, $9}'openvpn --genkey tls-crypt-v2-server v2-server.keyopenvpn --tls-crypt-v2 v2-server.key --genkey tls-crypt-v2-client v2-laptop1.key bGFwdG9wMQ==wc -c v2-server.key v2-laptop1.keyecho bGFwdG9wMQ== | base64 -d
Output
## 2048 bit OpenVPN static key#-----BEGIN OpenVPN Static key V1-----636 static.key636 ta.key636 tc.key265 token.key      269 v2-server.key      846 v2-laptop1.keylaptop1
  • genkey secret, tls-auth and tls-crypt produce the same 636-byte static key format.
  • auth-token key signs the server's session tokens.
  • tls-crypt-v2: one server key, then a wrapped key per client (metadata laptop1 as base64).

117.5 TLS mode options (53)

Output
# server.confca pki/ca.crtcert pki/vpn-server.crtkey pki/vpn-server.keydh nonetls-crypt-v2 keys/v2-server.keyremote-cert-tls clienttls-version-min 1.2# client.ovpnclienttls-crypt-v2 keys/v2-laptop1.keyremote-cert-tls serververify-x509-name vpn-server name
Output
[server] Control Channel: using tls-crypt-v2 key[server] VERIFY OK: depth=1, CN=N117 Lab CA[server] VERIFY EKU OK[server] VERIFY OK: depth=0, CN=laptop1VERIFY OK: depth=1, CN=N117 Lab CAVERIFY KU OKVERIFY EKU OKVERIFY X509NAME OK: CN=vpn-serverVERIFY OK: depth=0, CN=vpn-serverControl Channel: TLSv1.3, cipher TLSv1.3 TLS_AES_256_GCM_SHA384, peer certificate: 256 bits ECprime256v1[vpn-server] Peer Connection Initiated with [AF_INET]192.168.137.10:1194
  • dh none = only ECDH; remote-cert-tls client/server checks the peer really has a client/server certificate (EKU).
  • verify-x509-name checks the server CN; the control channel ran on TLSv1.3.
Terminal
timeout 40 openvpn --config client.ovpn \  --verify-x509-name office-gw name \  --tls-exit --hand-window 10 \  --suppress-timestamps | grep -E 'VERIFY|TLS Error|TLS_ERROR|exiting|Exiting'
Output
VERIFY OK: depth=1, CN=N117 Lab CAVERIFY KU OKVERIFY EKU OK!VERIFY X509NAME ERROR: CN=vpn-server, must be office-gwTLS_ERROR: BIO read tls_read_plaintext errorTLS Error: TLS object -> incoming plaintext read errorTLS Error: TLS handshake failedSIGTERM[soft,tls-error] received, process exiting

Wrong expected name: VERIFY X509NAME ERROR, and --tls-exit makes openvpn exit instead of retrying: handy for monitoring scripts.

Output
--tls-server | --tls-client  handshake role--ca --cert --key   PEM files;  --pkcs12 f (all in one file)--crl-verify file | dir     reject revoked certificates--dh file|none --ecdh-curve --tls-groups--askpass [file]            private key password--tls-auth ta.key 0|1       HMAC sign only--tls-crypt tc.key          encrypt + auth--tls-crypt-v2 f [force-cookie]   per-client key--tls-version-min|max  --tls-cert-profile  --tls-cipher  --tls-ciphersuites--hand-window n --tls-timeout n --tls-exit--remote-cert-ku|eku|tls  --verify-hash  --peer-fingerprint--verify-x509-name n subject|name|prefix
OptionSigns (HMAC)Encrypts control channelKey
tls-authYesNoShared by all
tls-cryptYesYes (hides cert details)Shared by all
tls-crypt-v2YesYesOne per client

117.6 PKCS#11 / smart card options (15)

  • The private key lives on a smart card or USB token and never touches the disk.
  • --show-pkcs11-ids gives the certificate ID to pass to --pkcs11-id. This Alpine build has no PKCS11 flag, so the option is unrecognized.
Output
--pkcs11-providers lib.so     load provider--show-pkcs11-ids [lib] [1]   list IDs--pkcs11-id 'serialized-id'   which cert--pkcs11-pin-cache sec        PIN cache--pkcs11-protected-authentication 0|1
Output
$ openvpn --show-pkcs11-ids /usr/lib/opensc-pkcs11.so; echo exit=$?!Options error: Unrecognized option or missing!  or extra parameter(s) in [CMD-LINE]:1:!  show-pkcs11-ids (2.6.20)exit=1

117.7 Data channel renegotiation (5)

Output
--reneg-sec 3600       default: every hour--reneg-sec 3600 3000  server: random in range--reneg-sec 0          time-based off--reneg-bytes n        after n bytes--reneg-pkts n         after n packetslab client: --reneg-pkts 40
Output
$ ping -q -c 60 -i 0.2 -W 2 192.168.137.15060 packets transmitted, 60 packets received, 0% packet loss$ grep -n -E 'soft reset|...' /tmp/c1.log36:TLS: soft reset sec=31/3600 bytes=5464/-1 pkts=66/4045:TLS: soft reset sec=4/3600 bytes=4240/-1 pkts=40/4054:TLS: soft reset sec=4/3600 bytes=4240/-1 pkts=40/40
  • One data key forever is risky; default renegotiation is every 3600 s.
  • reneg-bytes / reneg-pkts trigger on traffic; the lower value on either peer wins.

117.8 Link options (46)

Terminal
# server.conf: port 1194 / proto udp / keepalive 2 10# client.ovpn: remote 192.168.137.10 1194 / nobindnetstat -lnup 2>/dev/null | grep 1194      # serverkill $(cat /tmp/server.pid)                # stop the servergrep -E 'Inactivity|SIGUSR1|restart' /tmp/c3.log   # client log
Output
udp  0  0 0.0.0.0:1194  0.0.0.0:*  85/openvpn[client] UDPv4 link local: (not bound)[client] UDPv4 link remote: [AF_INET]192.168.137.10:1194[client] PUSH_REPLY,... ping 2,ping-restart 10,...[client] Timers: ping 2, ping-restart 10[server] Timers: ping 2, ping-restart 20[vpn-server] Inactivity timeout (--ping-restart), restartingSIGUSR1[soft,ping-restart] received, process restarting
  • nobind: link local not bound, the OS picks a random source port.
  • keepalive 2 10 pushes ping 2 / ping-restart 10 to the client; 10 s after the server stopped, the client restarted (Inactivity timeout).
Output
--proto udp|tcp-client|tcp-server (4|6)--port n  --lport n  --rport n   (1194)--local host --bind --nobind--float           peer IP/port may change--keepalive i t = --ping i + --ping-restart t (server pushes, doubles t for itself)--ping-exit n     --ping-restart 0 (client default off)--mssfix max      --fragment max (UDP only)  --mtu-disc--replay-window n [t]  --session-timeout n  --mode p2p|server

117.9 tun/tap adapter, addressing and routing (69)

Terminal
# server.confdev tuntopology subnetserver 10.8.0.0 255.255.255.0tun-mtu 1400push "route 192.168.137.128 255.255.255.128"push "dhcp-option DNS 10.8.0.1"# clientip -4 addr show tun0ip routeping -c 3 -W 2 192.168.137.150
Output
PUSH: Received control message: 'PUSH_REPLY,route 192.168.137.128 255.255.255.128,  dhcp-option DNS 10.8.0.1,route-gateway 10.8.0.1,topology subnet,ping 2,ping-restart 10,  ifconfig 10.8.0.2 255.255.255.0,...,tun-mtu 1400'3: tun0: <POINTOPOINT,MULTICAST,NOARP,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UNKNOWN    inet 10.8.0.2/24 brd 10.8.0.255 scope global tun0default via 192.168.137.1 dev eth010.8.0.0/24 dev tun0 proto kernel scope link src 10.8.0.2192.168.137.0/25 dev eth0 proto kernel scope link src 192.168.137.20192.168.137.128/25 via 10.8.0.1 dev tun064 bytes from 192.168.137.150: seq=0 ttl=63 time=28.240 ms3 packets transmitted, 3 packets received, 0% packet loss
  • dev tun = layer 3 device; dev tap = layer 2 Ethernet. topology subnet = one IP per client from the pool.
  • tun-mtu 1400 set tun0's MTU; push route added 192.168.137.128/25 via tun0; ttl=63 because the VPN server is one hop.
Split tunnel vs redirect-gateway def1
  1. 1.Split tunnel: only 192.168.137.128/25 is routed into tun0

  2. 2.Packet to 1.1.1.1 leaves directly via eth0

  3. 3.--redirect-gateway def1 adds 0.0.0.0/1 and 128.0.0.0/1 via tun0

  4. 4.Host route to VPN server 192.168.137.10 via old gateway (no loop)

  5. 5.Now 1.1.1.1 goes via tun0 to the VPN server, then the internet

  6. 6.Client stops: these routes are removed

def1 does not replace the default route; two /1 routes are more specific

Terminal
openvpn --cd /etc/openvpn/n117 --config client.ovpn \  --redirect-gateway def1 --daemon --log /tmp/c2.log --suppress-timestamps --writepid /tmp/c2.pidip routeip route get 1.1.1.1ip route get 192.168.137.10kill $(cat /tmp/c2.pid); sleep 2; ip route
Output
0.0.0.0/1 via 10.8.0.1 dev tun0default via 192.168.137.1 dev eth010.8.0.0/24 dev tun0 proto kernel scope link ...128.0.0.0/1 via 10.8.0.1 dev tun0192.168.137.0/25 dev eth0 proto kernel ...192.168.137.10 via 192.168.137.1 dev eth0192.168.137.128/25 via 10.8.0.1 dev tun0$ ip route get 1.1.1.11.1.1.1 via 10.8.0.1 dev tun0 src 10.8.0.2$ ip route get 192.168.137.10192.168.137.10 via 192.168.137.1 dev eth0 src 192.168.137.20$ kill $(cat /tmp/c2.pid); sleep 2; ip routedefault via 192.168.137.1 dev eth0192.168.137.0/25 dev eth0 proto kernel scope link src 192.168.137.20
Output
--dev tun|tap|tunN|null --dev-type tun|tap--dev-node /dev/net/tun | utun | "TAP"--topology net30|p2p|subnet --lladdr MAC--ifconfig local remote | addr mask   --ifconfig-ipv6 a/bits--route net mask gw metric  (gw: vpn_gateway net_gateway remote_host)--route-gateway gw|dhcp --route-metric m --route-nopull--redirect-gateway def1 local autolocal bypass-dhcp bypass-dns block-local ipv6 !ipv4--redirect-private --block-ipv6--dhcp-option DNS|DOMAIN|NTP|WINS ...--tun-mtu n

117.10 Common mistakes

  • Treating tls-auth and tls-crypt as the same: tls-auth only signs, tls-crypt also encrypts.
  • Expecting all traffic in the VPN when the server pushes only a route: that is a split tunnel; add redirect-gateway def1.
  • Skipping remote-cert-tls / verify-x509-name: another client could pose as the server.
  • Leaving --ping-restart off on clients (default 0): dead tunnels never restart; use keepalive on the server.

117.11 Interview tips

  • Stop MITM: remote-cert-tls server + verify-x509-name vpn-server name + tls-crypt-v2 + tls-version-min 1.2 + crl-verify on the server.
  • redirect-gateway def1 = 0.0.0.0/1 + 128.0.0.0/1 + host route to the server.
  • keepalive 10 60 style = ping every 10 s, restart after 60 s silence.
  • tls-crypt-v2: a leaked client key does not force re-keying every client.