Setup an IKEv2 server with StrongSwan

Setup an IKEv2 server with StrongSwan

more private for your public network

IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunneling between the server and client. In IKEv2 implementations, IPSec provides encryption for the network traffic. IKEv2 is natively supported on some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary, and it handles client hiccups quite smoothly.

This tutorial is adapted from this post with little customisations. Also, contexts were based on a Ubuntu 18.04 server.

Installing StrongSwan

First, we’ll install StrongSwan, an open-source IPSec daemon which we’ll configure in our server. We’ll also install the public key infrastructure component so that we can create a certificate authority to provide credentials for our infrastructure.

Update the local package cache and install the software by typing:

sudo apt update
sudo apt install strongswan strongswan-pki

Now that everything’s installed, let’s move on to creating our certificates.

Creating a certificate authority

An IKEv2 server requires a certificate to identify itself to clients. To help us create the certificate required, the strongswan-pki package comes with a utility to generate a certificate authority and server certificates. To begin, let’s create a few directories to store all the assets we’ll be working on. The directory structure matches some of the directories in /etc/ipsec.d, where we will eventually move all of the items we create. We’ll lock down the permissions so that our private files can’t be seen by other users:

mkdir -p ~/pki/{cacerts,certs,private}
chmod 700 ~/pki

Now that we have a directory structure to store everything, we can generate a root key. This will be a 4096-bit RSA key that will be used to sign our root certificate authority.

Execute these commands to generate the key:

ipsec pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/ca-key.pem

Now that we have a key, we can move on to creating our root certificate authority, using the key to sign the root certificate:

ipsec pki --self --ca --lifetime 3650 --in ~/pki/private/ca-key.pem \
    --type rsa --dn "CN=StrongSwan root CA" --outform pem > ~/pki/cacerts/ca-cert.pem

You can change the distinguished name (DN) values to something else to if you would like (start with CN=). The common name here is just the indicator, so it doesn’t have to match anything in your infrastructure.

Now that we’ve got our root certificate authority up and running, we can create a certificate that the StrongSwan server will use.

Generating a certificate for the StrongSwan server

We’ll now create a certificate and key for the StrongSwan server. This certificate will allow the client to verify the server’s authenticity using the CA certificate we just generated.

First, create a private key for the StrongSwan server with the following command:

ipsec pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/server-key.pem

Now, create and sign the StrongSwan server certificate with the certificate authority’s key you created in the previous step. Execute the following command, but change the Common Name (CN) and the Subject Alternate Name (SAN) field to your server’s DNS name or IP address:

ipsec pki --pub --in ~/pki/private/server-key.pem --type rsa \
    | ipsec pki --issue --lifetime 1825 \
        --cacert ~/pki/cacerts/ca-cert.pem \
        --cakey ~/pki/private/ca-key.pem \
        --dn "CN=server_domain_or_IP" --san "server_domain_or_IP" \
        --flag serverAuth --flag ikeIntermediate --outform pem \
    >  ~/pki/certs/server-cert.pem

Now that we’ve generated all of the TLS/SSL files StrongSwan needs, we can move the files into place in the /etc/ipsec.d directory by typing:

sudo cp -r ~/pki/* /etc/ipsec.d/

And you can safely delete the pki folder.

Configuring StrongSwan

StrongSwan has a default configuration file with some examples, but we will have to do most of the configuration ourselves. Let’s back up the file for reference before starting from scratch:

sudo mv /etc/ipsec.conf{,.original}

Create and open a new blank configuration file by typing:

sudo nano /etc/ipsec.conf

First, we’ll tell StrongSwan to log daemon statuses for debugging and allow duplicate connections. Add these lines to the file:

config setup
    charondebug="ike 1, knl 1, cfg 0"
    uniqueids=no

Then, we’ll create a configuration section for our server. We’ll also tell StrongSwan to create IKEv2 Tunnels and to automatically load this configuration section when it starts up. Append the following lines to the file:

. . .
conn ikev2
    auto=add
    compress=no
    type=tunnel
    keyexchange=ikev2
    fragmentation=yes
    forceencaps=yes

We’ll also configure dead-peer detection to clear any “dangling” connections in case the client unexpectedly disconnects. Add these lines:

. . .
conn ikev2
    . . .
    dpdaction=clear
    dpddelay=300s
    rekey=no

Then, we’ll configure the server (left) side IPSec parameters. Add this to the file:

. . .
conn ikev2
    . . .
    left=%any
    leftid=@server_domain_or_IP
    leftcert=server-cert.pem
    leftsendcert=always
    leftsubnet=0.0.0.0/0

Note: When configuring the server ID (leftid), only include the @ character if your StrongSwan server will be identified by a domain name:

leftid=@domain.example.com

If the server will be identified by its IP address, just put the IP address in:

leftid=1.1.1.1

Next, we can configure the client (right) side IPSec parameters, like the private IP address ranges and DNS servers to use:

conn ikev2
    . . .
    right=%any
    rightid=%any
    rightauth=eap-mschapv2
    rightsourceip=10.10.10.0/24
    rightdns=8.8.8.8,8.8.4.4
    rightsendcert=never

Finally, we’ll tell StrongSwan to ask the client for user credentials when they connect:

conn ikev2
    . . .
    eap_identity=%identity

The whole configuration file should look like this:

config setup
    charondebug="ike 1, knl 1, cfg 0"
    uniqueids=no

conn ikev2
    auto=add
    compress=no
    type=tunnel
    keyexchange=ikev2
    fragmentation=yes
    forceencaps=yes
    dpdaction=clear
    dpddelay=300s
    rekey=no
    left=%any
    leftid=@server_domain_or_IP
    leftcert=server-cert.pem
    leftsendcert=always
    leftsubnet=0.0.0.0/0
    right=%any
    rightid=%any
    rightauth=eap-mschapv2
    rightsourceip=10.10.10.0/24
    rightdns=8.8.8.8,8.8.4.4
    rightsendcert=never
    eap_identity=%identity

Save and close the file once you’ve verified that you’ve configured things as shown.

Configuring StrongSwan authentication

Our IKEv2 server is now configured to accept client connections, but we don’t have any credentials configured yet. We’ll need to configure a couple things in a special configuration file called ipsec.secrets:

  • We need to tell StrongSwan where to find the private key for our server certificate, so the server will be able to authenticate to clients.
  • We also need to set up a list of users that will be allowed to connect to the VPN.

Let’s open the secrets file for editing:

sudo nano /etc/ipsec.secrets

First, we’ll tell StrongSwan where to find our private key:

: RSA "server-key.pem"

Then, we’ll define the user credentials. You can make up any username or password combination that you like:

your_username : EAP "your_password"

Save and close the file. Now that we’ve finished working with the parameters, we’ll restart the StrongSwan service so that our configuration is applied:

sudo systemctl restart strongswan

Configuring the firewall & kernel IP forwarding

With the StrongSwan configuration complete, we need to configure the firewall to forward and allow network traffic through.

First, allow UDP traffic to the standard IPSec ports, 500 and 4500. I configured this in the server provider’s web interface. If you’re using UFW, please refer to this section.

Next, we will edit the iptables with a few low-level policies for routing and forwarding IPSec packets. Before we do, we need to find which network interface on our server is used for internet access.

ip route | grep default

Your public interface should follow the word “dev”, ens3 for example.

When you have your public network interface, add the following configuration block:

sudo iptables -t nat -A POSTROUTING -o ens3 -m policy --pol ipsec --dir out -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o ens3 -j MASQUERADE

sudo iptables -t mangle -A FORWARD --match policy --pol ipsec --dir in -o ens3 -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360

Change each instance of ens3 in the above configuration to match the interface name you found with ip route. The -t nat lines create rules so that the firewall can correctly route and manipulate traffic between the clients and the internet. The -t mangle line adjusts the maximum packet segment size to prevent potential issues with certain clients.

Next, we’ll change some network kernel parameters to allow routing from one interface to another. Open kernel parameters configuration file:

sudo nano /etc/sysctl.conf

We’ll need to configure a few things here:

  • First, we’ll enable IPv4 packet forwarding.
  • We’ll disable Path MTU discovery to prevent packet fragmentation problems.
  • We also won’t accept ICMP redirects nor send ICMP redirects to prevent man-in-the-middle attacks.

Add or uncommnet these lines in sysctl.conf file:

. . .

# Enable forwarding
# Uncomment the following line
net/ipv4/ip_forward=1

. . .

# Do not accept ICMP redirects (prevent MITM attacks)
# Ensure the following line is set
net/ipv4/conf/all/accept_redirects=0

# Do not send ICMP redirects (we are not a router)
# Add the following lines
net/ipv4/conf/all/send_redirects=0
net/ipv4/ip_no_pmtu_disc=1

Save the file when you are finished.

Connection on macOS, iOS, and Windows

Now that you have everything set up, it’s time to try it out. First, you’ll need to copy the CA certificate you created and install it on your client device(s) that will connect to the StrongSwan server. The easiest way to do this is to log into your server and output the contents of the certificate file:

cat /etc/ipsec.d/cacerts/ca-cert.pem

You’ll see output similar to this:

-----BEGIN CERTIFICATE-----
MIIFQjCCAyqgAwIBAgIIFkQGvkH4ej0wDQYJKoZIhvcNAQEMBQAwPzELMAkGA1UE

. . .

EwbVLOXcNduWK2TPbk/+82GRMtjftran6hKbpKGghBVDPVFGFT6Z0OfubpkQ9RsQ
BayqOb/Q
-----END CERTIFICATE-----

Copy this output to your computer, including the -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- lines, and save it to a file with a recognizable name, such as ca-cert.pem. Ensure the file you create has the .pem extension.

Connecting from macOS

Follow these steps to import the certificate:

  • Double-click the certificate file. Keychain Access will pop up with a dialog that says “Keychain Access is trying to modify the system keychain. Enter your password to allow this.”
  • Enter your password, then click on Modify Keychain
  • Double-click the newly imported certificate (display name as setted CN name, StrongSwan root CA as previous settings). This brings up a small properties window where you can specify the trust levels. Set IP Security (IPSec) to Always Trust and you’ll be prompted for your password again. This setting saves automatically after entering the password.

Now that the certificate is important and trusted, configure the network connection with these steps:

  • Go to System Preferences and choose Network.
  • Click on the small “plus” button on the lower-left of the list of networks.
  • In the popup that appears, Set Interface to VPN, set the VPN Type to IKEv2, and give the connection a name.
  • In the Server Address and Remote ID field, enter the server’s domain name or IP address. Leave the Local ID blank.
  • Click on Authentication Settings, select Username, and enter your username and password you configured. Then click OK.

Finally, click on Connect to connect to the IKEv2 tunnel. You should now be connected to the StrongSwan server.

Connecting from iOS

To configure the StrongSwan connection on an iOS device, follow these steps:

  • Send yourself an email with the root certificate attached.
  • Open the email on your iOS device and tap on the attached certificate file, then tap Install and enter your passcode. Once it installs, tap Done.
  • Go to Settings, General, VPN and tap Add VPN Configuration. This will bring up the VPN connection configuration screen.
  • Tap on Type and select IKEv2.
  • In the Description field, enter a short name for the VPN connection. This could be anything you like.
  • In the Server and Remote ID field, enter the server’s domain name or IP address. The Local ID field can be left blank.
  • Enter your username and password in the Authentication section, then tap Done.
  • Select the VPN connection that you just created, tap the switch on the top of the page, and you’ll be connected.

Connecting from Windows

First, import the root certificate by following these steps:

  1. Press WINDOWS+R to bring up the Run dialog, and enter mmc.exe to launch the Windows Management Console.
  2. From the File menu, navigate to Add or Remove Snap-in, select Certificates from the list of available snap-ins, and click Add.
  3. We want the StrongSwan to work with any user, so select Computer Account and click Next.
  4. We’re configuring things on the local computer, so select Local Computer, then click Finish.
  5. Under the Console Root node, expand the Certificates (Local Computer) entry, expand Trusted Root Certification Authorities, and then select the Certificates entry.
  6. From the Action menu, select All Tasks and click Import to display the Certificate Import Wizard. Click Next to move past the introduction.
  7. On the File to Import screen, press the Browse button and select the certificate file that you’ve saved. Then click Next.
  8. Ensure that the Certificate Store is set to Trusted Root Certification Authorities, and click Next.
  9. Click Finish to import the certificate.

Then configure the network connection with these steps:

  • Launch Control Panel, then navigate to the Network and Sharing Center.
  • Click on Set up a new connection or network, then select Connect to a workplace.
  • Select Use my Internet connection (VPN).
  • Enter the StrongSwan server details. Enter the server’s domain name or IP address in the Internet address field, then fill in Destination name with something that describes your StrongSwan connection. Then click Done.

Your new StrongSwan connection will be visible under the list of networks. Select the VPN and click Connect. You’ll be prompted for your username and password. Type them in, click OK, and you’ll be connected.

Conclusion

In this post, you’ve built a StrongSwan server that uses the IKEv2 protocol. Now you can be assured that your online activities will remain secure wherever you go!

This is a much easier implementation than OpenConnect that a single click brought me to a secure network environment.

林宏

Frank Lin

Hey, there! This is Frank Lin (@flinhong), one of the 1.4 billion 🇨🇳. This 'inDev. Journal' site holds the exploration of my quirky thoughts and random adventures through life. Hope you enjoy reading and perusing my posts.

YOU MAY ALSO LIKE

Setting up snap Nextcloud on Ubuntu

Tutorials

2019.12.05

Setting up snap Nextcloud on Ubuntu

Nextcloud, a fork of ownCloud, is a open-source file sharing server that allows you to store your personal content, like documents and pictures, in a centralized location, much like Dropbox. It also returns the control and security of your sensitive data back to you, thus eliminating the use of a third-party cloud hosting service. Here, I'm going to walk through the installing and configurations on Ubuntu 18.04 using the snappy packaging system.