App Support.

We're here to help.

YubiKey U2F Two-Factor Authentication with OpenVPN and Viscosity

After setting up your own OpenVPN server, you may want to enhance it's security. One way to do that is to use 2FA (Two Factor Authentication). This adds another security measure to prevent unwanted users connecting to your server. One type of 2FA is U2F (Universal Two Factor) with a YubiKey. This guide will expand on setting up an OpenVPN server on Ubuntu by adding U2F support to that server using Viscosity's built in U2F support which was added in version 1.7.7.

Preparation

For this guide, we assume:

You have already installed the latest version of Ubuntu LTS (16.04 or 18.04 at time of writing)

Your OpenVPN server has a Fully Qualified Domain Name (FQDN), more information below.

This guide assumes you have followed one of our server setup guides and you are already able to connect to the server we will be modifying using certificate/key authentication. This guide will add two more authentication steps. A username and password using PAM, and a challenge request using a YubiKey's U2F support.

PAM authentication is the simplest form of username/password authentication we can use with OpenVPN. PAM uses the Ubuntu's user management to authenticate against so we don't need to manage an extra database of username and passwords. If you want to add a new user to be able to authenticate, you can simply add the new user with the useradd command in Ubuntu.

A final requirement to support U2F is your server must have a FQDN. U2F requires that you must connect to your OpenVPN server with a domain name, not an IP Address. A dynamic DNS service is fine if you are connecting back to your home For a server you are hosting on a VPS though for example, we recommend you register a domain address, something like myserver.mydomain.com.

Ubuntu

Modifying OpenVPN

As this is a newer type of 2FA, OpenVPN needs some modifications to support U2F. We've done the heavy lifting for you for Ubuntu 16.04 and 18.04, however if you would like to view the patch, or recompile OpenVPN yourself, you can download the patches here and here, or view them in our PPA.

First, remove your current OpenVPN install and any other apt repo's you have added for OpenVPN, we're assuming you've followed our Ubuntu guide:

To confirm you've installed the right version of OpenVPN, during the last step you should see a version of OpenVPN installed containing 'u2f', for example:

Setting up openvpn (2.4.7-1ubuntu1u2f2)

Installing U2F Requirements

Now OpenVPN will support U2F, we can configure the server for U2F. The plugin uses a python script which interfaces with the u2fval package, install it with the following:

sudo pip install u2fval u2fval-client pam

Once this is all done, we can setup our U2F server, u2fval. This server handles authorising and registering U2F tokens for us. First, create the configuration for u2fval by running the following commands:

u2fval is a REST API. If we were using it for multiple services, we could host it with Apache or NGINX, but as we're using it on the same machine as our OpenVPN server, running it locally is fine. We'll create a service so u2fval runs on it's own and at startup. Create a new service with:

Setting up OpenVPN

Now all the requirements we need are installed and setup, we can interface U2F with OpenVPN. First, we need to modify our OpenVPN server configuration. Make a copy of the existing server and edit the configuration:

That's it, our server is ready to go with U2F support! We just need to make a quick change to our configuration, read on.

Setting up Viscosity

Now the server is setup, we only need to make a single change to Viscosity:

Open Viscosity's Preferences and edit your connection.

Go to the Authentication tab and tick 'Use Username/Password authentication'

Save the connection

If during this guide you registered a dynamic DNS or domain name for your OpenVPN server, also change your servers IP address to the new address.

Using Viscosity

Now that the changes to your configuration in Viscosity are finished, you can connect to the VPN server to test the connection. The first time you connect you'll be prompted to register your U2F security key. Simply follow the on-screen instructions by connecting your key to your computer, and if required tapping the gold disk or button on the device to activate and register it.

Once you have registered your device, or if it is already registered, you'll be prompted to authenticate using your security key. Again, simply make sure your key is connected to your computer, and if required tap the gold disk or button on the device to activate it and authenticate. Your VPN connection will then proceed to connect if authentication was successful.