- Connecting multiple IKEv2 clients from behind the same NAT requires setting the "local ID" field to match the client name. Ref: https://github.com/libreswan/libreswan/issues/237
19 KiB
Step-by-Step Guide: How to Set Up IKEv2 VPN
Read this in other languages: English, 简体中文.
Note: This guide is for advanced users. Other users please use IPsec/L2TP or IPsec/XAuth mode.
- Introduction
- Using helper scripts
- Manually set up IKEv2 on the VPN server
- Configure IKEv2 VPN clients
- Add a client certificate
- Revoke a client certificate
- Known issues
- References
Introduction
Modern operating systems (such as Windows 7 and newer) support the IKEv2 standard. Internet Key Exchange (IKE or IKEv2) is the protocol used to set up a Security Association (SA) in the IPsec protocol suite. Compared to IKE version 1, IKEv2 contains improvements such as Standard Mobility support through MOBIKE, and improved reliability.
Libreswan can authenticate IKEv2 clients on the basis of X.509 Machine Certificates using RSA signatures. This method does not require an IPsec PSK, username or password. It can be used with:
- Windows 7, 8.x and 10
- OS X (macOS)
- Android 4.x and newer (using the strongSwan VPN client)
- iOS (iPhone/iPad)
Using helper scripts
Important: As a prerequisite to using this guide, and before continuing, you must make sure that you have successfully set up your own VPN server, and (optional but recommended) upgraded Libreswan to the latest version. Docker users, see here.
You may use this helper script to automatically set up IKEv2 on the VPN server:
wget https://git.io/ikev2setup -O ikev2.sh && sudo bash ikev2.sh
The script must be run using bash
, not sh
. Follow the prompts to set up IKEv2. When finished, continue to configure IKEv2 VPN clients and check known issues. If you want to generate certificates for additional VPN clients, just run the script again.
Manually set up IKEv2 on the VPN server
The following example shows how to manually configure IKEv2 with Libreswan. Commands below must be run as root
.
-
Find the VPN server's public IP, save it to a variable and check.
PUBLIC_IP=$(dig @resolver1.opendns.com -t A -4 myip.opendns.com +short) [ -z "$PUBLIC_IP" ] && PUBLIC_IP=$(wget -t 3 -T 15 -qO- http://ipv4.icanhazip.com) printf '%s\n' "$PUBLIC_IP"
Check to make sure the output matches the server's public IP. This variable is required in the steps below.
Note: Alternatively, you may specify the server's DNS name here. e.g.
PUBLIC_IP=myvpn.example.com
. -
Add a new IKEv2 connection:
if ! grep -qs '^include /etc/ipsec\.d/\*\.conf$' /etc/ipsec.conf; then echo >> /etc/ipsec.conf echo 'include /etc/ipsec.d/*.conf' >> /etc/ipsec.conf fi
cat > /etc/ipsec.d/ikev2.conf <<EOF conn ikev2-cp left=%defaultroute leftcert=$PUBLIC_IP leftid=@$PUBLIC_IP leftsendcert=always leftsubnet=0.0.0.0/0 leftrsasigkey=%cert right=%any rightid=%fromcert rightaddresspool=192.168.43.10-192.168.43.250 rightca=%same rightrsasigkey=%cert narrowing=yes dpddelay=30 dpdtimeout=120 dpdaction=clear auto=add ikev2=insist rekey=no pfs=no ike-frag=yes ike=aes256-sha2,aes128-sha2,aes256-sha1,aes128-sha1,aes256-sha2;modp1024,aes128-sha1;modp1024 phase2alg=aes_gcm-null,aes128-sha1,aes256-sha1,aes128-sha2,aes256-sha2 EOF
We need to add a few more lines to that file. First check your Libreswan version, then run one of the following commands:
ipsec --version
For Libreswan 3.23 and newer:
cat >> /etc/ipsec.d/ikev2.conf <<EOF modecfgdns="8.8.8.8 8.8.4.4" encapsulation=yes mobike=no EOF
Note: If your server (or Docker host) runs Debian or CentOS/RHEL and you wish to enable MOBIKE support, replace
mobike=no
withmobike=yes
in the command above. DO NOT enable this option on Ubuntu systems.For Libreswan 3.19-3.22:
cat >> /etc/ipsec.d/ikev2.conf <<EOF modecfgdns1=8.8.8.8 modecfgdns2=8.8.4.4 encapsulation=yes EOF
For Libreswan 3.18 and older:
cat >> /etc/ipsec.d/ikev2.conf <<EOF modecfgdns1=8.8.8.8 modecfgdns2=8.8.4.4 forceencaps=yes EOF
-
Generate Certificate Authority (CA) and VPN server certificates.
Note: Specify the certificate validity period (in months) with "-v". e.g. "-v 120".
Generate CA certificate:
certutil -z <(head -c 1024 /dev/urandom) \ -S -x -n "IKEv2 VPN CA" \ -s "O=IKEv2 VPN,CN=IKEv2 VPN CA" \ -k rsa -g 4096 -v 120 \ -d sql:/etc/ipsec.d -t "CT,," -2
Generating key. This may take a few moments... Is this a CA certificate [y/N]? y Enter the path length constraint, enter to skip [<0 for unlimited path]: > Is this a critical extension [y/N]? N
Generate VPN server certificate:
Note: If you specified the server's DNS name (instead of its IP address) in step 1 above, you must replace
--extSAN "ip:$PUBLIC_IP,dns:$PUBLIC_IP"
in the command below with--extSAN "dns:$PUBLIC_IP"
.certutil -z <(head -c 1024 /dev/urandom) \ -S -c "IKEv2 VPN CA" -n "$PUBLIC_IP" \ -s "O=IKEv2 VPN,CN=$PUBLIC_IP" \ -k rsa -g 4096 -v 120 \ -d sql:/etc/ipsec.d -t ",," \ --keyUsage digitalSignature,keyEncipherment \ --extKeyUsage serverAuth \ --extSAN "ip:$PUBLIC_IP,dns:$PUBLIC_IP"
Generating key. This may take a few moments...
-
Generate client certificate(s), then export the
.p12
file that contains the client certificate, private key, and CA certificate.Note: You may repeat this step to generate certificates for additional VPN clients, but make sure to replace every
vpnclient
withvpnclient2
, etc. To connect multiple VPN clients simultaneously, you must generate a unique certificate for each.Generate client certificate:
certutil -z <(head -c 1024 /dev/urandom) \ -S -c "IKEv2 VPN CA" -n "vpnclient" \ -s "O=IKEv2 VPN,CN=vpnclient" \ -k rsa -g 4096 -v 120 \ -d sql:/etc/ipsec.d -t ",," \ --keyUsage digitalSignature,keyEncipherment \ --extKeyUsage serverAuth,clientAuth -8 "vpnclient"
Generating key. This may take a few moments...
Export
.p12
file:pk12util -d sql:/etc/ipsec.d -n "vpnclient" -o vpnclient.p12
Enter password for PKCS12 file: Re-enter password: pk12util: PKCS12 EXPORT SUCCESSFUL
Enter a secure password to protect the exported
.p12
file (when importing into an iOS or macOS device, this password cannot be empty). -
(For iOS clients) Export the CA certificate as
ikev2vpnca.cer
:certutil -L -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" -a -o ikev2vpnca.cer
-
The database should now contain:
certutil -L -d sql:/etc/ipsec.d
Certificate Nickname Trust Attributes SSL,S/MIME,JAR/XPI IKEv2 VPN CA CTu,u,u ($PUBLIC_IP) u,u,u vpnclient u,u,u
Note: To display a certificate, use
certutil -L -d sql:/etc/ipsec.d -n "Nickname"
. To revoke a client certificate, follow these steps. For othercertutil
usage, read here. -
(Important) Restart the IPsec service:
service ipsec restart
Before continuing, you must restart the IPsec service. The IKEv2 setup on the VPN server is now complete. Follow instructions below to configure your VPN clients.
Configure IKEv2 VPN clients
Read this in other languages: English, 简体中文.
Note: If you specified the server's DNS name (instead of its IP address) in step 1 above, you must enter the DNS name in the Server and Remote ID fields. If you want to generate certificates for additional VPN clients, just run the helper script again. Or you may refer to step 4 in the previous section.
Windows 7, 8.x and 10
-
Securely transfer the generated
.p12
file to your computer, then import it into the "Computer account" certificate store. Make sure that the client cert is placed in "Personal -> Certificates", and the CA cert is placed in "Trusted Root Certification Authorities -> Certificates".Detailed instructions:
https://wiki.strongswan.org/projects/strongswan/wiki/Win7Certs -
On the Windows computer, add a new IKEv2 VPN connection:
https://wiki.strongswan.org/projects/strongswan/wiki/Win7Config -
Start the new VPN connection, and enjoy your IKEv2 VPN!
https://wiki.strongswan.org/projects/strongswan/wiki/Win7Connect -
(Optional) Enable stronger ciphers by adding the registry key
NegotiateDH2048_AES256
and reboot. Read more here.
OS X (macOS)
First, securely transfer the generated .p12
file to your Mac, then double-click to import into the login keychain in Keychain Access. Next, double-click on the imported IKEv2 VPN CA
certificate, expand Trust and select Always Trust from the IP Security (IPsec) drop-down menu. When finished, check to make sure both the new client certificate and IKEv2 VPN CA
are listed under the Certificates category of login keychain.
- Open System Preferences and go to the Network section.
- Click the + button in the lower-left corner of the window.
- Select VPN from the Interface drop-down menu.
- Select IKEv2 from the VPN Type drop-down menu.
- Enter anything you like for the Service Name.
- Click Create.
- Enter
Your VPN Server IP
(or DNS name) for the Server Address. - Enter
Your VPN Server IP
(or DNS name) for the Remote ID. - Enter
Your VPN client name
in the Local ID field.
Note: This must match exactly the client name you specified during IKEv2 setup. Same as the first part of your.p12
filename. - Click the Authentication Settings... button.
- Select None from the Authentication Settings drop-down menu.
- Select the Certificate radio button, then select the new client certificate.
- Click OK.
- Check the Show VPN status in menu bar checkbox.
- Click Apply to save the VPN connection information.
- Click Connect.
iOS
First, securely transfer the generated ikev2vpnca.cer
and .p12
files to your iOS device, then import them one by one as iOS profiles. To transfer the files, you may use:
- AirDrop, or
- Upload to your device, tap them in the "Files" app (must first move to the "On My iPhone" folder), then follow the prompts to import, or
- Host the files on a secure website of yours, then download and import them in Mobile Safari.
When finished, check to make sure both the new client certificate and IKEv2 VPN CA
are listed under Settings -> General -> Profiles.
- Go to Settings -> General -> VPN.
- Tap Add VPN Configuration....
- Tap Type. Select IKEv2 and go back.
- Tap Description and enter anything you like.
- Tap Server and enter
Your VPN Server IP
(or DNS name). - Tap Remote ID and enter
Your VPN Server IP
(or DNS name). - Enter
Your VPN client name
in the Local ID field.
Note: This must match exactly the client name you specified during IKEv2 setup. Same as the first part of your.p12
filename. - Tap User Authentication. Select None and go back.
- Make sure the Use Certificate switch is ON.
- Tap Certificate. Select the new client certificate and go back.
- Tap Done.
- Slide the VPN switch ON.
Android 10 and newer
- Securely transfer the generated
.p12
file to your Android device. - Install strongSwan VPN Client from Google Play.
- Launch the Settings application.
- Go to Security -> Advanced -> Encryption & credentials.
- Tap Install from storage (or SD card).
- Choose the
.p12
file you transferred from the VPN server, and follow the prompts.
Note: To find the.p12
file, click on the three-line menu button, then click on your device name. - Launch the strongSwan VPN client and tap Add VPN Profile.
- Enter
Your VPN Server IP
(or DNS name) in the Server field. - Select IKEv2 Certificate from the VPN Type drop-down menu.
- Tap Select user certificate, select the new client certificate and confirm.
- (Important) Tap Show advanced settings. Scroll down, find and enable the Use RSA/PSS signatures option.
- Save the new VPN connection, then tap to connect.
Android 4.x to 9.x
- Securely transfer the generated
.p12
file to your Android device. - Install strongSwan VPN Client from Google Play.
- Launch the strongSwan VPN client and tap Add VPN Profile.
- Enter
Your VPN Server IP
(or DNS name) in the Server field. - Select IKEv2 Certificate from the VPN Type drop-down menu.
- Tap Select user certificate, then tap Install certificate.
- Choose the
.p12
file you transferred from the VPN server, and follow the prompts.
Note: To find the.p12
file, click on the three-line menu button, then click on your device name. - (Important) Tap Show advanced settings. Scroll down, find and enable the Use RSA/PSS signatures option.
- Save the new VPN connection, then tap to connect.
Once successfully connected, you can verify that your traffic is being routed properly by looking up your IP address on Google. It should say "Your public IP address is Your VPN Server IP
".
Add a client certificate
If you want to generate certificates for additional VPN clients, just run the helper script again. Or you may refer to step 4 in this section.
Revoke a client certificate
In certain circumstances, you may need to revoke a previously generated VPN client certificate. This can be done using crlutil
. See example steps below, commands must be run as root
.
-
Check the database, and identify the nickname of the client certificate you want to revoke.
certutil -L -d sql:/etc/ipsec.d
Certificate Nickname Trust Attributes SSL,S/MIME,JAR/XPI IKEv2 VPN CA CTu,u,u ($PUBLIC_IP) u,u,u vpnclient-to-revoke u,u,u
In this example, we will revoke the certificate with nickname
vpnclient-to-revoke
, issued byIKEv2 VPN CA
. -
Find the serial number of this client certificate.
certutil -L -d sql:/etc/ipsec.d -n "vpnclient-to-revoke"
Certificate: Data: Version: 3 (0x2) Serial Number: 00:cd:69:ff:74 ... ...
From the output, we see that the serial number is
CD69FF74
in hexadecimal, which is3446275956
in decimal. It will be used in the next steps. -
Create a new Certificate Revocation List (CRL). You only need to do this once for each CA.
if ! crlutil -L -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" 2>/dev/null; then crlutil -G -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" -c /dev/null fi
CRL Info: : Version: 2 (0x1) Signature Algorithm: PKCS #1 SHA-256 With RSA Encryption Issuer: "O=IKEv2 VPN,CN=IKEv2 VPN CA" This Update: Sat Jun 06 22:00:00 2020 CRL Extensions:
-
Add the client certificate you want to revoke to the CRL. Here we specify the certificate's serial number in decimal, and the revocation time in GeneralizedTime format (YYYYMMDDhhmmssZ) in UTC.
crlutil -M -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" <<EOF addcert 3446275956 20200606220100Z EOF
CRL Info: : Version: 2 (0x1) Signature Algorithm: PKCS #1 SHA-256 With RSA Encryption Issuer: "O=IKEv2 VPN,CN=IKEv2 VPN CA" This Update: Sat Jun 06 22:02:00 2020 Entry 1 (0x1): Serial Number: 00:cd:69:ff:74 Revocation Date: Sat Jun 06 22:01:00 2020 CRL Extensions:
Note: If you want to remove a certificate from the CRL, replace
addcert 3446275956 20200606220100Z
above withrmcert 3446275956
. For othercrlutil
usage, read here. -
Finally, let Libreswan re-read the updated CRL.
ipsec crls
Known issues
- The built-in VPN client in Windows may not support IKEv2 fragmentation. On some networks, this can cause the connection to fail or have other issues. You may instead try the IPsec/L2TP or IPsec/XAuth mode.
- Ubuntu 18.04 users may encounter the error "The password you entered is incorrect" when trying to import the generated
.p12
file into Windows. This is due to a bug inNSS
. Read more here. - If using the strongSwan Android VPN client, you must upgrade Libreswan on your server to version 3.26 or above.
References
- https://libreswan.org/wiki/VPN_server_for_remote_clients_using_IKEv2
- https://libreswan.org/wiki/HOWTO:_Using_NSS_with_libreswan
- https://libreswan.org/man/ipsec.conf.5.html
- https://wiki.strongswan.org/projects/strongswan/wiki/WindowsClients
- https://wiki.strongswan.org/projects/strongswan/wiki/AndroidVpnClient
- https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/NSS_Tools_certutil
- https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/NSS_Tools_crlutil