1
0
mirror of synced 2024-11-22 21:16:02 +03:00
setup-ipsec-vpn/docs/ikev2-howto.md

675 lines
33 KiB
Markdown
Raw Normal View History

2021-01-31 08:52:14 +03:00
# Guide: How to Set Up and Use IKEv2 VPN
2016-08-28 08:41:46 +03:00
*Read this in other languages: [English](ikev2-howto.md), [简体中文](ikev2-howto-zh.md).*
2021-01-18 20:03:39 +03:00
**Note:** You may also connect using [IPsec/L2TP](clients.md) or [IPsec/XAuth](clients-xauth.md) mode.
2019-02-10 01:24:19 +03:00
* [Introduction](#introduction)
* [Using helper scripts](#using-helper-scripts)
2019-02-10 01:24:19 +03:00
* [Configure IKEv2 VPN clients](#configure-ikev2-vpn-clients)
* [Manage client certificates](#manage-client-certificates)
2020-12-30 01:36:44 +03:00
* [Manually set up IKEv2 on the VPN server](#manually-set-up-ikev2-on-the-vpn-server)
2019-02-10 01:24:19 +03:00
* [Known issues](#known-issues)
2020-12-30 01:36:44 +03:00
* [Remove IKEv2](#remove-ikev2)
2019-02-10 01:24:19 +03:00
* [References](#references)
2016-09-10 00:45:39 +03:00
2019-02-10 01:24:19 +03:00
## 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 <a href="https://en.wikipedia.org/wiki/Internet_Key_Exchange#Improvements_with_IKEv2" target="_blank">improvements</a> such as Standard Mobility support through MOBIKE, and improved reliability.
2016-08-28 08:41:46 +03:00
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:
2016-08-28 08:41:46 +03:00
- Windows 7, 8.x and 10
- OS X (macOS)
- iOS (iPhone/iPad)
2021-01-31 08:52:14 +03:00
- Android 4.x and newer (using the strongSwan VPN client)
2020-12-27 09:16:49 +03:00
After following this guide, you will be able to connect to the VPN using IKEv2 in addition to the existing [IPsec/L2TP](clients.md) and [IPsec/XAuth ("Cisco IPsec")](clients-xauth.md) modes.
## Using helper scripts
**New:** For macOS and iOS clients, the helper script can now create .mobileconfig files to simplify client setup and improve VPN performance.
2021-01-18 20:03:39 +03:00
**Important:** Before continuing, you should have successfully <a href="https://github.com/hwdsl2/setup-ipsec-vpn" target="_blank">set up your own VPN server</a>, and (optional but recommended) <a href="../README.md#upgrade-libreswan" target="_blank">updated Libreswan</a>. **Docker users, see <a href="https://github.com/hwdsl2/docker-ipsec-vpn-server/blob/master/README.md#configure-and-use-ikev2-vpn" target="_blank">here</a>**.
2019-02-10 01:24:19 +03:00
2021-01-18 20:03:39 +03:00
Use this helper script to automatically set up IKEv2 on the VPN server:
```
2021-01-18 09:02:04 +03:00
wget https://git.io/ikev2setup -O ikev2.sh && sudo bash ikev2.sh --auto
```
2021-01-18 20:03:39 +03:00
The <a href="../extras/ikev2setup.sh" target="_blank">script</a> must be run using `bash`, not `sh`. The command above runs the helper script in auto mode, using default options. Remove the `--auto` parameter if you want to customize IKEv2 setup options. When finished, continue to [configure IKEv2 VPN clients](#configure-ikev2-vpn-clients).
2021-01-25 00:55:26 +03:00
<details>
<summary>
Click here to view detailed usage information for the IKEv2 helper script.
</summary>
```
2021-01-25 05:01:40 +03:00
Usage: bash ikev2.sh [options]
2021-01-25 00:55:26 +03:00
Options:
--auto run IKEv2 setup in auto mode using default options (for initial IKEv2 setup only)
--addclient [client name] add a new IKEv2 client using default options (after IKEv2 setup)
--exportclient [client name] export an existing IKEv2 client using default options (after IKEv2 setup)
--listclients list the names of existing IKEv2 clients (after IKEv2 setup)
2021-01-26 07:32:45 +03:00
--removeikev2 remove IKEv2 and delete all certificates and keys from the IPsec database
2021-01-25 00:55:26 +03:00
-h, --help show this help message and exit
2021-01-25 05:01:40 +03:00
To customize IKEv2 or client options, run this script without arguments.
2021-01-25 00:55:26 +03:00
```
</details>
2020-12-30 01:36:44 +03:00
## Configure IKEv2 VPN clients
*Read this in other languages: [English](ikev2-howto.md#configure-ikev2-vpn-clients), [简体中文](ikev2-howto-zh.md#配置-ikev2-vpn-客户端).*
2021-01-25 00:55:26 +03:00
**Note:** If you want to generate certificates for additional VPN clients, or export configuration for an existing client, just run the [helper script](#using-helper-scripts) again. Use option `-h` to show detailed usage information.
2020-12-30 01:36:44 +03:00
* [Windows 7, 8.x and 10](#windows-7-8x-and-10)
* [OS X (macOS)](#os-x-macos)
* [iOS (iPhone/iPad)](#ios)
2021-01-24 01:05:51 +03:00
* [Android](#android)
2020-12-30 01:36:44 +03:00
### Windows 7, 8.x and 10
1. Securely transfer the generated `.p12` file to your computer, then import it into the "Computer account" certificate store. To import the `.p12` file, run the following from an <a href="http://www.winhelponline.com/blog/open-elevated-command-prompt-windows/" target="_blank">elevated command prompt</a>:
2020-12-30 01:36:44 +03:00
```console
2021-01-29 08:54:32 +03:00
# Import .p12 file (replace with your own value)
certutil -f -importpfx "\path\to\your\file.p12" NoExport
```
2021-01-31 08:52:14 +03:00
Alternatively, you can manually import the `.p12` file. Click <a href="https://wiki.strongswan.org/projects/strongswan/wiki/Win7Certs" target="_blank">here</a> for instructions. Make sure that the client cert is placed in "Personal -> Certificates", and the CA cert is placed in "Trusted Root Certification Authorities -> Certificates".
2020-12-30 01:36:44 +03:00
2021-01-31 08:52:14 +03:00
**Note:** Ubuntu 18.04 users may encounter the error "The password you entered is incorrect" when trying to import the `.p12` file. See [Known issues](#known-issues).
2021-01-21 10:39:05 +03:00
2021-01-31 08:52:14 +03:00
1. On the Windows computer, add a new IKEv2 VPN connection. For Windows 8.x and 10, it is recommended to create the VPN connection using the following commands from a command prompt, for improved security and performance.
```console
2021-01-29 08:54:32 +03:00
# Set server address (replace with your own value)
set server_addr="Your VPN Server IP (or DNS name)"
# Create VPN connection
2021-01-29 08:54:32 +03:00
powershell -command "Add-VpnConnection -Name 'My IKEv2 VPN' -ServerAddress '%server_addr%' -TunnelType IKEv2 -AuthenticationMethod MachineCertificate -EncryptionLevel Required -PassThru"
# Set IPsec configuration
2021-01-29 08:54:32 +03:00
powershell -command "Set-VpnConnectionIPsecConfiguration -ConnectionName 'My IKEv2 VPN' -AuthenticationTransformConstants GCMAES256 -CipherTransformConstants GCMAES256 -EncryptionMethod AES256 -IntegrityCheckMethod SHA256 -PfsGroup None -DHGroup Group14 -PassThru -Force"
```
2021-01-31 08:52:14 +03:00
Alternatively, you can manually create the VPN connection. Click <a href="https://wiki.strongswan.org/projects/strongswan/wiki/Win7Config" target="_blank">here</a> for instructions. If you specified the server's DNS name (instead of its IP address) during IKEv2 setup, you must enter the DNS name in the **Internet address** field.
2020-12-30 01:36:44 +03:00
2021-01-29 08:54:32 +03:00
1. Enable stronger ciphers for IKEv2 with a one-time registry change. This is optional, but recommended. Download and import the `.reg` file below, or run the following from an elevated command prompt. Read more <a href="https://wiki.strongswan.org/projects/strongswan/wiki/WindowsClients#AES-256-CBC-and-MODP2048" target="_blank">here</a>.
2020-12-30 01:36:44 +03:00
- For Windows 7, 8.x and 10 ([download .reg file](https://dl.ls20.com/reg-files/v1/Enable_Stronger_Ciphers_for_IKEv2_on_Windows.reg))
```console
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\RasMan\Parameters /v NegotiateDH2048_AES256 /t REG_DWORD /d 0x1 /f
```
2021-01-29 08:54:32 +03:00
To connect to the VPN: Click on the wireless/network icon in your system tray, select the new VPN entry, and click **Connect**. Once successfully connected, you can verify that your traffic is being routed properly by <a href="https://www.google.com/search?q=my+ip" target="_blank">looking up your IP address on Google</a>. It should say "Your public IP address is `Your VPN Server IP`".
2020-12-30 01:36:44 +03:00
2021-01-29 08:54:32 +03:00
If you get an error when trying to connect, see <a href="clients.md#troubleshooting" target="_blank">Troubleshooting</a>.
2020-12-30 01:36:44 +03:00
### OS X (macOS)
First, securely transfer the generated `.mobileconfig` file to your Mac, then double-click and follow the prompts to import as a macOS profile. When finished, check to make sure "IKEv2 VPN configuration" is listed under System Preferences -> Profiles.
2021-01-29 08:54:32 +03:00
To connect to the VPN:
1. Open System Preferences and go to the Network section.
1. Select the VPN connection with `Your VPN Server IP` (or DNS name).
1. Check the **Show VPN status in menu bar** checkbox.
1. Click **Connect**.
(Optional feature) You can choose to enable <a href="https://developer.apple.com/documentation/networkextension/personal_vpn/vpn_on_demand_rules" target="_blank">VPN On Demand</a>. This is an "always-on" feature that can automatically connect to the VPN while on Wi-Fi. To enable, check the **Connect on demand** checkbox for the VPN connection, and click **Apply**.
<details>
<summary>
2021-01-19 10:42:29 +03:00
If you manually set up IKEv2 without using the helper script, click here for instructions.
</summary>
2020-12-30 01:36:44 +03:00
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. Close the dialog using the red "X" on the top-left corner. When prompted, use Touch ID or enter your password and click "Update Settings".
When finished, check to make sure both the new client certificate and `IKEv2 VPN CA` are listed under the **Certificates** category of **login** keychain.
1. Open System Preferences and go to the Network section.
1. Click the **+** button in the lower-left corner of the window.
1. Select **VPN** from the **Interface** drop-down menu.
1. Select **IKEv2** from the **VPN Type** drop-down menu.
1. Enter anything you like for the **Service Name**.
1. Click **Create**.
2021-01-24 01:05:51 +03:00
1. Enter `Your VPN Server IP` (or DNS name) for the **Server Address**.
**Note:** If you specified the server's DNS name (instead of its IP address) during IKEv2 setup, you must enter the DNS name in the **Server Address** and **Remote ID** fields.
2020-12-30 01:36:44 +03:00
1. Enter `Your VPN Server IP` (or DNS name) for the **Remote ID**.
1. 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.
1. Click the **Authentication Settings...** button.
1. Select **None** from the **Authentication Settings** drop-down menu.
1. Select the **Certificate** radio button, then select the new client certificate.
1. Click **OK**.
1. Check the **Show VPN status in menu bar** checkbox.
1. Click **Apply** to save the VPN connection information.
1. Click **Connect**.
</details>
Once successfully connected, you can verify that your traffic is being routed properly by <a href="https://www.google.com/search?q=my+ip" target="_blank">looking up your IP address on Google</a>. It should say "Your public IP address is `Your VPN Server IP`".
2020-12-30 01:36:44 +03:00
2021-01-29 08:54:32 +03:00
If you get an error when trying to connect, see <a href="clients.md#troubleshooting" target="_blank">Troubleshooting</a>.
2020-12-30 01:36:44 +03:00
### iOS
First, securely transfer the generated `.mobileconfig` file to your iOS device, then import it as an iOS profile. To transfer the file, you may use:
1. AirDrop, or
1. Upload to your device using "File Sharing" in iTunes, then open the "Files" app on your iOS device, move the uploaded file to the "On My iPhone" folder. After that, tap the file and go to "Settings" to import, or
1. Host the file on a secure website of yours, then download and import it in Mobile Safari.
When finished, check to make sure "IKEv2 VPN configuration" is listed under Settings -> General -> Profile(s).
2021-01-29 08:54:32 +03:00
To connect to the VPN:
1. Go to Settings -> General -> VPN.
1. Select the VPN connection with `Your VPN Server IP` (or DNS name).
1. Slide the **VPN** switch ON.
(Optional feature) You can choose to enable <a href="https://developer.apple.com/documentation/networkextension/personal_vpn/vpn_on_demand_rules" target="_blank">VPN On Demand</a>. This is an "always-on" feature that can automatically connect to the VPN while on Wi-Fi. To enable, tap the "i" icon on the right of the VPN connection, and enable **Connect On Demand**.
<details>
<summary>
2021-01-19 10:42:29 +03:00
If you manually set up IKEv2 without using the helper script, click here for instructions.
</summary>
2020-12-30 01:36:44 +03:00
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:
1. AirDrop, or
1. Upload to your device using "File Sharing" in iTunes, then open the "Files" app on your iOS device, move the uploaded files to the "On My iPhone" folder. After that, tap each file and go to "Settings" to import, or
2020-12-30 01:36:44 +03:00
1. 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.
1. Go to Settings -> General -> VPN.
1. Tap **Add VPN Configuration...**.
1. Tap **Type**. Select **IKEv2** and go back.
1. Tap **Description** and enter anything you like.
2021-01-24 01:05:51 +03:00
1. Tap **Server** and enter `Your VPN Server IP` (or DNS name).
**Note:** If you specified the server's DNS name (instead of its IP address) during IKEv2 setup, you must enter the DNS name in the **Server** and **Remote ID** fields.
2020-12-30 01:36:44 +03:00
1. Tap **Remote ID** and enter `Your VPN Server IP` (or DNS name).
1. 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.
1. Tap **User Authentication**. Select **None** and go back.
1. Make sure the **Use Certificate** switch is ON.
1. Tap **Certificate**. Select the new client certificate and go back.
1. Tap **Done**.
1. Slide the **VPN** switch ON.
</details>
Once successfully connected, you can verify that your traffic is being routed properly by <a href="https://www.google.com/search?q=my+ip" target="_blank">looking up your IP address on Google</a>. It should say "Your public IP address is `Your VPN Server IP`".
2020-12-30 01:36:44 +03:00
2021-01-29 08:54:32 +03:00
If you get an error when trying to connect, see <a href="clients.md#troubleshooting" target="_blank">Troubleshooting</a>.
2021-01-24 01:05:51 +03:00
### Android
1. Securely transfer the generated `.sswan` file to your Android device.
1. Install <a href="https://play.google.com/store/apps/details?id=org.strongswan.android" target="_blank">strongSwan VPN Client</a> from **Google Play**.
1. Launch the strongSwan VPN client.
1. Tap the "more options" menu on top right, then tap **Import VPN profile**.
1. Choose the `.sswan` file you transferred from the VPN server.
**Note:** To find the `.sswan` file, tap the three-line menu button, then browse to the location you saved the file.
1. On the "Import VPN profile" screen, tap **IMPORT CERTIFICATE FROM VPN PROFILE**, and follow the prompts.
1. On the "Choose certificate" screen, select the new client certificate, then tap **Select**.
1. Tap **IMPORT**.
1. Tap the new VPN profile to connect.
2021-01-29 08:54:32 +03:00
(Optional feature) You can choose to enable the "Always-on VPN" feature on Android. Launch the **Settings** app, go to Network & internet -> Advanced -> VPN, click the gear icon on the right of "strongSwan VPN Client", then enable the **Always-on VPN** and **Block connections without VPN** options.
2021-01-24 01:05:51 +03:00
<details>
<summary>
If you manually set up IKEv2 without using the helper script, click here for instructions.
</summary>
**Android 10 and newer:**
2020-12-30 01:36:44 +03:00
1. Securely transfer the generated `.p12` file to your Android device.
1. Install <a href="https://play.google.com/store/apps/details?id=org.strongswan.android" target="_blank">strongSwan VPN Client</a> from **Google Play**.
1. Launch the **Settings** application.
1. Go to Security -> Advanced -> Encryption & credentials.
2021-01-24 01:05:51 +03:00
1. Tap **Install certificates from storage (or SD card)**.
2020-12-30 01:36:44 +03:00
1. Choose the `.p12` file you transferred from the VPN server, and follow the prompts.
2021-01-24 01:05:51 +03:00
**Note:** To find the `.p12` file, tap the three-line menu button, then browse to the location you saved the file.
2020-12-30 01:36:44 +03:00
1. Launch the strongSwan VPN client and tap **Add VPN Profile**.
2021-01-24 01:05:51 +03:00
1. Enter `Your VPN Server IP` (or DNS name) in the **Server** field.
**Note:** If you specified the server's DNS name (instead of its IP address) during IKEv2 setup, you must enter the DNS name in the **Server** field.
2020-12-30 01:36:44 +03:00
1. Select **IKEv2 Certificate** from the **VPN Type** drop-down menu.
1. Tap **Select user certificate**, select the new client certificate and confirm.
1. **(Important)** Tap **Show advanced settings**. Scroll down, find and enable the **Use RSA/PSS signatures** option.
1. Save the new VPN connection, then tap to connect.
2021-01-24 01:05:51 +03:00
**Android 4 to 9:**
2020-12-30 01:36:44 +03:00
1. Securely transfer the generated `.p12` file to your Android device.
1. Install <a href="https://play.google.com/store/apps/details?id=org.strongswan.android" target="_blank">strongSwan VPN Client</a> from **Google Play**.
1. Launch the strongSwan VPN client and tap **Add VPN Profile**.
2021-01-24 01:05:51 +03:00
1. Enter `Your VPN Server IP` (or DNS name) in the **Server** field.
**Note:** If you specified the server's DNS name (instead of its IP address) during IKEv2 setup, you must enter the DNS name in the **Server** field.
2020-12-30 01:36:44 +03:00
1. Select **IKEv2 Certificate** from the **VPN Type** drop-down menu.
1. Tap **Select user certificate**, then tap **Install certificate**.
1. Choose the `.p12` file you transferred from the VPN server, and follow the prompts.
2021-01-24 01:05:51 +03:00
**Note:** To find the `.p12` file, tap the three-line menu button, then browse to the location you saved the file.
2020-12-30 01:36:44 +03:00
1. **(Important)** Tap **Show advanced settings**. Scroll down, find and enable the **Use RSA/PSS signatures** option.
1. Save the new VPN connection, then tap to connect.
2021-01-24 01:05:51 +03:00
</details>
2020-12-30 01:36:44 +03:00
Once successfully connected, you can verify that your traffic is being routed properly by <a href="https://www.google.com/search?q=my+ip" target="_blank">looking up your IP address on Google</a>. It should say "Your public IP address is `Your VPN Server IP`".
2021-01-29 08:54:32 +03:00
If you get an error when trying to connect, see <a href="clients.md#troubleshooting" target="_blank">Troubleshooting</a>.
## Manage client certificates
2021-01-25 00:55:26 +03:00
### List existing clients
If you want to list the names of existing IKEv2 clients, run the [helper script](#using-helper-scripts) with the `--listclients` option.
<details>
<summary>
Click here to view detailed usage information for the IKEv2 helper script.
</summary>
```
2021-01-25 05:01:40 +03:00
Usage: bash ikev2.sh [options]
2021-01-25 00:55:26 +03:00
Options:
--auto run IKEv2 setup in auto mode using default options (for initial IKEv2 setup only)
--addclient [client name] add a new IKEv2 client using default options (after IKEv2 setup)
--exportclient [client name] export an existing IKEv2 client using default options (after IKEv2 setup)
--listclients list the names of existing IKEv2 clients (after IKEv2 setup)
2021-01-26 07:32:45 +03:00
--removeikev2 remove IKEv2 and delete all certificates and keys from the IPsec database
2021-01-25 00:55:26 +03:00
-h, --help show this help message and exit
2021-01-25 05:01:40 +03:00
To customize IKEv2 or client options, run this script without arguments.
2021-01-25 00:55:26 +03:00
```
</details>
### Add a client certificate
2020-12-30 01:36:44 +03:00
2021-01-25 00:55:26 +03:00
To generate certificates for additional IKEv2 clients, just run the [helper script](#using-helper-scripts) again. Refer to the usage information above. Or you may refer to step 4 in [this section](#manually-set-up-ikev2-on-the-vpn-server).
2020-12-30 01:36:44 +03:00
2021-01-19 07:49:55 +03:00
### Export configuration for an existing client
2020-12-30 01:36:44 +03:00
2021-01-25 00:55:26 +03:00
By default, the [IKEv2 helper script](#using-helper-scripts) exports client configuration after running. If later you want to export configuration for an existing client, run the helper script again and select the appropriate option. Refer to the usage information above.
2020-12-30 01:36:44 +03:00
### Revoke a client certificate
2020-12-30 01:36:44 +03:00
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`.
1. Check the database, and identify the nickname of the client certificate you want to revoke.
```bash
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 by `IKEv2 VPN CA`.
1. Find the serial number of this client certificate.
```bash
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 is `3446275956` in decimal. It will be used in the next steps.
1. Create a new Certificate Revocation List (CRL). You only need to do this once for each CA.
```bash
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:
```
1. 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.
```bash
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 with `rmcert 3446275956`. For other `crlutil` usage, read <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/NSS_Tools_crlutil" target="_blank">here</a>.
1. Finally, let Libreswan re-read the updated CRL.
```bash
ipsec crls
```
## Manually set up IKEv2 on the VPN server
2020-12-30 01:36:44 +03:00
As an alternative to using the [helper script](#using-helper-scripts), advanced users can manually set up IKEv2. The following example shows how to manually configure IKEv2 with Libreswan. Commands below must be run as `root`.
2016-09-10 00:45:39 +03:00
1. Find the VPN server's public IP, save it to a variable and check.
2016-08-28 08:41:46 +03:00
```bash
2018-11-23 20:52:38 +03:00
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"
2016-08-28 08:41:46 +03:00
```
2018-11-23 20:52:38 +03:00
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`.
2020-05-31 07:13:14 +03:00
1. Add a new IKEv2 connection:
2016-08-28 08:41:46 +03:00
```bash
2020-05-31 07:13:14 +03:00
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
```
**Note:** If you specified the server's DNS name (instead of its IP address) in step 1 above, you must replace `leftid=$PUBLIC_IP` in the command below with `leftid=@$PUBLIC_IP`.
2020-05-31 07:13:14 +03:00
```bash
cat > /etc/ipsec.d/ikev2.conf <<EOF
2016-08-28 08:41:46 +03:00
conn ikev2-cp
left=%defaultroute
2016-08-28 08:41:46 +03:00
leftcert=$PUBLIC_IP
leftid=$PUBLIC_IP
2016-08-28 08:41:46 +03:00
leftsendcert=always
leftsubnet=0.0.0.0/0
leftrsasigkey=%cert
right=%any
rightid=%fromcert
rightaddresspool=192.168.43.10-192.168.43.250
2016-08-28 08:41:46 +03:00
rightca=%same
rightrsasigkey=%cert
narrowing=yes
dpddelay=30
dpdtimeout=120
dpdaction=clear
auto=add
ikev2=insist
rekey=no
pfs=no
fragmentation=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
ikelifetime=24h
salifetime=24h
2016-08-28 08:41:46 +03:00
EOF
```
We need to add a few more lines to that file. First check your Libreswan version, then run one of the following commands:
2017-01-17 02:27:08 +03:00
```bash
2018-11-23 20:52:38 +03:00
ipsec --version
```
For Libreswan 3.23 and newer:
2018-02-11 09:37:00 +03:00
```bash
2020-05-31 07:13:14 +03:00
cat >> /etc/ipsec.d/ikev2.conf <<EOF
modecfgdns="8.8.8.8 8.8.4.4"
2018-02-11 09:37:00 +03:00
encapsulation=yes
mobike=no
2018-02-11 09:37:00 +03:00
EOF
```
2020-12-30 01:36:44 +03:00
**Note:** The <a href="https://wiki.strongswan.org/projects/strongswan/wiki/MobIke" target="_blank">MOBIKE</a> IKEv2 extension allows VPN clients to change network attachment points, e.g. switch between mobile data and Wi-Fi and keep the IPsec tunnel up on the new IP. If your server (or Docker host) is **NOT** running Ubuntu Linux, and you wish to enable MOBIKE support, replace `mobike=no` with `mobike=yes` in the command above. **DO NOT** enable this option on Ubuntu systems or Raspberry Pis.
For Libreswan 3.19-3.22:
```bash
2020-05-31 07:13:14 +03:00
cat >> /etc/ipsec.d/ikev2.conf <<EOF
modecfgdns1=8.8.8.8
modecfgdns2=8.8.4.4
2018-02-11 09:37:00 +03:00
encapsulation=yes
EOF
```
For Libreswan 3.18 and older:
```bash
2020-05-31 07:13:14 +03:00
cat >> /etc/ipsec.d/ikev2.conf <<EOF
2018-02-11 09:37:00 +03:00
modecfgdns1=8.8.8.8
modecfgdns2=8.8.4.4
forceencaps=yes
EOF
2017-01-17 02:27:08 +03:00
```
2020-05-03 09:59:37 +03:00
1. Generate Certificate Authority (CA) and VPN server certificates.
**Note:** Specify the certificate validity period (in months) with "-v". e.g. "-v 120".
2016-08-28 08:41:46 +03:00
Generate CA certificate:
2016-08-28 08:41:46 +03:00
```bash
2018-11-23 20:52:38 +03:00
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
```
2016-08-28 08:41:46 +03:00
Generate VPN server certificate:
2018-10-22 04:25:34 +03:00
**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"`.
```bash
2018-11-23 20:52:38 +03:00
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...
2016-08-28 08:41:46 +03:00
```
2020-05-03 09:59:37 +03:00
1. Generate client certificate(s), then export the `.p12` file that contains the client certificate, private key, and CA certificate.
2016-08-28 08:41:46 +03:00
2020-05-31 07:13:14 +03:00
**Note:** You may repeat this step to generate certificates for additional VPN clients, but make sure to replace every `vpnclient` with `vpnclient2`, etc. To connect multiple VPN clients simultaneously, you must generate a unique certificate for each.
Generate client certificate:
2016-08-28 08:41:46 +03:00
```bash
2018-11-23 20:52:38 +03:00
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:
```bash
pk12util -d sql:/etc/ipsec.d -n "vpnclient" -o vpnclient.p12
```
2016-08-28 08:41:46 +03:00
```
Enter password for PKCS12 file:
Re-enter password:
pk12util: PKCS12 EXPORT SUCCESSFUL
2016-08-28 08:41:46 +03:00
```
Enter a secure password to protect the exported `.p12` file (when importing into an iOS or macOS device, this password cannot be empty).
2016-08-28 08:41:46 +03:00
2020-06-11 09:37:47 +03:00
1. (For iOS clients) Export the CA certificate as `ikev2vpnca.cer`:
```bash
2020-06-11 09:37:47 +03:00
certutil -L -d sql:/etc/ipsec.d -n "IKEv2 VPN CA" -a -o ikev2vpnca.cer
```
2016-08-28 08:41:46 +03:00
1. The database should now contain:
```bash
2018-11-23 20:52:38 +03:00
certutil -L -d sql:/etc/ipsec.d
```
2016-08-28 08:41:46 +03:00
```
Certificate Nickname Trust Attributes
SSL,S/MIME,JAR/XPI
2016-08-28 08:41:46 +03:00
IKEv2 VPN CA CTu,u,u
($PUBLIC_IP) u,u,u
vpnclient u,u,u
2016-08-28 08:41:46 +03:00
```
**Note:** To display a certificate, use `certutil -L -d sql:/etc/ipsec.d -n "Nickname"`. To revoke a client certificate, follow [these steps](#revoke-a-client-certificate). For other `certutil` usage, read <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/tools/NSS_Tools_certutil" target="_blank">here</a>.
2016-08-28 08:41:46 +03:00
1. **(Important) Restart the IPsec service**:
2016-08-28 08:41:46 +03:00
```bash
2018-11-23 20:52:38 +03:00
service ipsec restart
2016-08-28 08:41:46 +03:00
```
2020-12-30 01:36:44 +03:00
Before continuing, you **must** restart the IPsec service. The IKEv2 setup on the VPN server is now complete. Follow instructions to [configure VPN clients](#configure-ikev2-vpn-clients).
2020-12-30 01:36:44 +03:00
## Known issues
2020-12-30 01:36:44 +03:00
1. The built-in VPN client in Windows may not support IKEv2 fragmentation (this feature <a href="https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-ikee/74df968a-7125-431d-9c98-4ea929e548dc" target="_blank">requires</a> Windows 10 v1803 or newer). On some networks, this can cause the connection to fail or have other issues. You may instead try the <a href="clients.md" target="_blank">IPsec/L2TP</a> or <a href="clients-xauth.md" target="_blank">IPsec/XAuth</a> mode.
1. 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 in `NSS`. Read more <a href="https://github.com/hwdsl2/setup-ipsec-vpn/issues/414#issuecomment-460495258" target="_blank">here</a>.
2021-01-21 10:39:05 +03:00
<details>
<summary>
Workaround for the NSS bug on Ubuntu 18.04
</summary>
2021-01-24 01:05:51 +03:00
**Note:** This workaround should only be used on Ubuntu 18.04 systems running on the `x86_64` architecture. As of 2021-01-21, the IKEv2 helper script was updated to automatically apply this workaround.
2021-01-22 08:11:20 +03:00
2021-01-21 10:39:05 +03:00
First, install newer versions of `libnss3` related packages:
```
2021-01-22 08:11:20 +03:00
wget https://mirrors.kernel.org/ubuntu/pool/main/n/nss/libnss3_3.49.1-1ubuntu1.5_amd64.deb
wget https://mirrors.kernel.org/ubuntu/pool/main/n/nss/libnss3-dev_3.49.1-1ubuntu1.5_amd64.deb
wget https://mirrors.kernel.org/ubuntu/pool/universe/n/nss/libnss3-tools_3.49.1-1ubuntu1.5_amd64.deb
2021-01-21 10:39:05 +03:00
apt-get -y update
apt-get -y install "./libnss3_3.49.1-1ubuntu1.5_amd64.deb" \
"./libnss3-dev_3.49.1-1ubuntu1.5_amd64.deb" \
"./libnss3-tools_3.49.1-1ubuntu1.5_amd64.deb"
```
After that, [export configuration for the IKEv2 client](#export-configuration-for-an-existing-client) again.
</details>
2021-01-31 08:52:14 +03:00
1. If using the strongSwan Android VPN client, you must <a href="../README.md#upgrade-libreswan" target="_blank">update Libreswan</a> on your server to version 3.26 or above.
2020-12-30 01:36:44 +03:00
## Remove IKEv2
2021-01-30 23:31:37 +03:00
If you want to remove IKEv2 from the VPN server, but keep the [IPsec/L2TP](clients.md) and [IPsec/XAuth ("Cisco IPsec")](clients-xauth.md) modes, run the [helper script](#using-helper-scripts) again and select the "Remove IKEv2" option. Note that this will delete all IKEv2 configuration including certificates and keys, and **cannot be undone**!
2021-01-19 10:42:29 +03:00
<details>
<summary>
Alternatively, you can manually remove IKEv2. Click here for instructions.
</summary>
2021-01-30 23:31:37 +03:00
To manually remove IKEv2 from the VPN server, but keep the [IPsec/L2TP](clients.md) and [IPsec/XAuth ("Cisco IPsec")](clients-xauth.md) modes, follow these steps. Commands must be run as `root`. Note that this will delete all IKEv2 configuration including certificates and keys, and **cannot be undone**!
2020-12-30 01:36:44 +03:00
1. Rename (or delete) the IKEv2 config file:
```bash
2020-12-30 01:36:44 +03:00
mv /etc/ipsec.d/ikev2.conf /etc/ipsec.d/ikev2.conf.bak
```
2021-01-19 10:42:29 +03:00
**Note:** If you used an older version (before 2020-05-31) of the IKEv2 helper script or instructions, file `/etc/ipsec.d/ikev2.conf` may not exist. In this case, please instead remove the `conn ikev2-cp` section from file `/etc/ipsec.conf`.
2020-12-30 01:36:44 +03:00
1. **(Important) Restart the IPsec service**:
```bash
2020-12-30 01:36:44 +03:00
service ipsec restart
```
2020-12-30 01:36:44 +03:00
1. List certificates in the IPsec database:
```bash
2020-12-30 01:36:44 +03:00
certutil -L -d sql:/etc/ipsec.d
```
2020-12-30 01:36:44 +03:00
Example output:
```
2020-12-30 01:36:44 +03:00
Certificate Nickname Trust Attributes
SSL,S/MIME,JAR/XPI
2020-12-30 01:36:44 +03:00
IKEv2 VPN CA CTu,u,u
($PUBLIC_IP) u,u,u
vpnclient u,u,u
```
2021-01-31 08:52:14 +03:00
1. Delete certificates and keys. Replace "Nickname" below with each certificate's nickname. Repeat these commands for each certificate. When finished, list certificates in the IPsec database again, and confirm that the list is empty.
```bash
2021-01-31 08:52:14 +03:00
certutil -F -d sql:/etc/ipsec.d -n "Nickname"
certutil -D -d sql:/etc/ipsec.d -n "Nickname" 2>/dev/null
```
2021-01-19 10:42:29 +03:00
</details>
2016-09-10 00:45:39 +03:00
## References
2016-08-28 08:41:46 +03:00
* 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
2018-09-04 08:51:58 +03:00
* 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