Skip to content

TOTP/MFA integration for VPN: OpenVPN and strongSwan patterns

How to add TOTP-based MFA to OpenVPN and strongSwan VPN gateways — google-authenticator PAM, EAP-RADIUS, push-based providers, and the enrollment flow we run.

A certificate-only VPN is “something you have.” A username/password VPN is “something you know.” Adding TOTP gives you the second factor that compliance regimes — SOC 2 type II, ISO 27001, PCI-DSS — want for remote access. This article covers the two patterns we use in production: PAM-based TOTP for OpenVPN with pam_google_authenticator, and EAP-RADIUS for strongSwan IKEv2 with FreeRADIUS pulling TOTP from a backing store. Both end with a tested enrollment flow and a documented recovery path.

How to verify

Before adding MFA, check what auth the gateway is using today and confirm time sync is solid — TOTP fails silently on clock drift.

timedatectl status | grep -E 'synchronized|service'
grep -E '^plugin|^auth-user-pass-verify' /etc/openvpn/server/server.conf
swanctl --list-conns | grep -i auth
ls /etc/pam.d/openvpn 2>/dev/null

If timedatectl shows NTP service: inactive or a non-synced clock, fix that first. TOTP codes are 30-second windows; anything more than 60 seconds of drift breaks login.

What’s happening

TOTP (RFC 6238) is a hash of a shared secret plus the current time, truncated to 6 digits. The client (Google Authenticator, Authy, 1Password, hardware tokens like YubiKey OATH) and the server share the secret; both compute the same code from their respective clocks. The server accepts a code, marks the time window used, and rejects any reuse.

Two integration paths for VPNs:

  • PAM — the gateway runs username+password+TOTP through the system’s PAM stack. pam_google_authenticator does the TOTP check; whatever else PAM does (LDAP, local password) does the password check. OpenVPN integrates with PAM via the openvpn-plugin-auth-pam plugin. Simple, works without extra services, but the secrets live in ~/.google_authenticator files on the gateway.
  • RADIUS — the gateway delegates the auth question to a RADIUS server. FreeRADIUS can speak TOTP via rlm_totp or by chaining to an OAuth/Push provider. strongSwan’s eap-radius plugin is the standard integration. The secrets live in the RADIUS backing store (LDAP attribute, database, Vault) — better for fleet scale, more moving parts.

Push-based MFA (Duo, Okta Verify) replaces TOTP with an out-of-band notification. For VPNs we use RADIUS + Duo’s RADIUS proxy or Okta’s RADIUS agent; the user gets a phone notification instead of typing a code.

The procedure

Pattern A: OpenVPN with PAM TOTP

  1. Install the pieces.

    apt install -y libpam-google-authenticator openvpn-plugin-auth-pam
  2. Write a PAM stack at /etc/pam.d/openvpn:

    # Require local password OR LDAP, then TOTP.
    auth required pam_unix.so
    auth required pam_google_authenticator.so secret=/etc/openvpn/google-auth/${USER} user=root
  3. Enable the plugin in server.conf.

    plugin /usr/lib/openvpn/openvpn-plugin-auth-pam.so openvpn
    verify-client-cert require
    username-as-common-name

    With verify-client-cert require, you still need the client cert — the TOTP is the second factor.

  4. Enroll users. For each user, generate their TOTP seed:

    mkdir -p /etc/openvpn/google-auth
    sudo -u root google-authenticator -t -d -f -r 3 -R 30 -W \
      -s /etc/openvpn/google-auth/alice
    chmod 600 /etc/openvpn/google-auth/alice

    The command prints a QR code and emergency scratch codes. Show the QR code to the user (in person or over a secure channel); they scan into their authenticator app.

  5. Update the client .ovpn to prompt for credentials:

    auth-user-pass
    auth-nocache
    reneg-sec 0

    Some clients accept TOTP in the password field with a : separator (password:123456), others require a static-challenge prompt. We default to the static-challenge approach:

    static-challenge "Enter TOTP code" 1

Pattern B: strongSwan with EAP-RADIUS + FreeRADIUS TOTP

  1. Install FreeRADIUS with the TOTP module.

    apt install -y freeradius freeradius-utils freeradius-totp
  2. Configure /etc/freeradius/3.0/mods-enabled/totp (link from mods-available):

    totp {
        challenge_format = "Enter TOTP code"
        secret = "%{User-Profile:totpSecret}"
        time_skew = 30
    }
  3. Chain in /etc/freeradius/3.0/sites-enabled/default:

    authorize {
        ldap
        totp
    }
    authenticate {
        Auth-Type LDAP { ldap }
        Auth-Type TOTP { totp }
    }
  4. Point strongSwan at RADIUS. In /etc/strongswan.d/charon-eap-radius.conf:

    eap-radius {
        servers {
            radius {
                address = 127.0.0.1
                secret = SharedRadiusSecret
            }
        }
        accounting = yes
        station_id_with_port = no
    }

    In swanctl.conf, remote { auth = eap-radius } instead of auth = eap-mschapv2.

  5. Reload everything.

    systemctl restart freeradius
    swanctl --load-all --noprompt
  6. Test from a client. With strongSwan client on Linux:

    echo -n "alice@example.org" > /tmp/u
    echo -n "password:123456" > /tmp/p
    # Standard IKEv2 connection — TOTP is the second factor at EAP-MSCHAPv2-with-RADIUS step.

Common pitfalls

  • Clock drift kills TOTP silently. Run chronyd or systemd-timesyncd and alert on drift > 5 seconds.
  • Reusing the same TOTP secret across multiple users — ~/.google_authenticator is per-user and must be regenerated for each. Audit on a quarterly basis.
  • Skipping the recovery flow. When a user’s phone breaks, they need a way to get a new TOTP seed. Document: “go to ticketing, prove identity by SSO+webcam, an admin issues a new seed.”
  • Leaving emergency scratch codes in the user’s email — they should be printed and stored physically. Yes, we know, it is 2026.
  • Using PAM TOTP on a clustered gateway without sync — each gateway has its own ~/.google_authenticator files. Either sync them (rsync, NFS) or move to RADIUS where the secrets live in one place.

In the engagements we run, VPN MFA is non-optional for new builds — the compliance regimes that fund the Managed Operations engagement require it, and the pattern is well-trodden enough that the technical work is one ticket, not a project. What costs the time is enrollment, recovery, and the audit trail of who has what seed when.