convert to vimwiki
[robinkrens.nl] / docs / ikev2-nat-rw.txt
1 Strongswan road warrior setup with Virtual IPs
2 =======
3
4 strongSwan is an IPsec solution providing encryption and authentication to servers and clients. It can be used to secure communications with remote networks, so that connecting remotely is the same as connecting locally. In this HOWTO, I explain how to setup up a secure connection to your server. In this setup your host will be the *gateway*, you might have other servers behind this gateway you can then reach securily. In this particular setup we use public key authentication between a *roadwarrior* and your server. Roadwarriors is the term Strongswan uses for laptops or other mobile devices that connect from a remote location to your network. More on this particular setup can be found here: https://www.strongswan.org/testing/testresults/ikev2/mobike-virtual-ip-nat/index.html
5 Note: some distributions use *ipsec* as command, others use *strongswan*
6
7 ----------
8
9 Setup a PKI infrastructure 
10 **********
11
12 To set up a public key infrastructure (PKI), we first need to create a self-signed Certificate Authority (CA). We use StrongSwan's built-in command `ipsec pki`. Later on, our CA will issue end-entity certificates. Generate a private key for the CA:
13
14         ipsec pki --gen > caKey.der
15
16 Now self-sign a CA certificate using the generated key. Adjust the distinguished name (DN) to your needs, it will be included in all issued certificates.
17
18         ipsec pki --self --in caKey.der --dn "C=USA, O= , CN=Host CA" --ca > caCert.der
19
20 Generate a private key for your   host and use your CA to issue a certificate.
21
22         ipsec pki --gen > hostKey.der`
23         ipsec pki --pub --in hostKey.der | ipsec pki --issue --cacert caCert.der --cakey caKey.der --dn "C=USA, O=  CN=host" > hostCert.der` --san your_IP
24
25 Now place the created files in the following directories of your Host:
26
27         /etc/ipsec.d/private/hostKey.der
28         /etc/ipsec.d/certs/hostCert.der
29         /etc/ipsec.d/cacerts/caCert.der
30
31 Similar, we can generate a private key and issue a certiciate for our client. 
32
33         ipsec pki --gen > clientKey.der
34         ipsec pki --pub --in clientKey.der | ipsec pki --issue --cacert caCert.der --cakey caKey.der --dn "C=USA, O= , CN=client" > clientCert.der
35
36 On your client you will need the client key and certificate as well as your CA certificate. To make it a bit more convenient, you can wrap these files in one .p12 file using the following command: 
37
38         openssl rsa -inform der -outform pem -in peerKey.der -out peerKey.pem
39         openssl pkcs12 -in clientCert.pem -inkey clientKey.pem -certfile caCert.pem -export -out client.p12`
40
41 Configure strongSwan
42 **********
43
44 Your /etc/ipsec.conf configuration file on your host should contain the following:
45
46         config setup
47
48         conn %default
49                 ikelifetime=60m
50                 keylife=20m
51                 rekeymargin=3m
52                 keyingtries=1
53                 keyexchange=ikev2
54
55         conn virtualip
56                 leftsubnet=0.0.0.0/0
57                 #leftid=alpha
58                 #leftauth=pubkey
59                 #rightauth=pubkey
60                 #leftsendcert=always
61                 leftfirewall=yes
62                 right=%any
63                 rightdns=8.8.8.8,8.8.4.4
64                 rightsourceip=172.16.16.0/24
65                 auto=add
66
67 Edit your /etc/ipsec.secrets and add the following line:
68
69          : RSA hostKey.der
70
71 Please note that both sides of the colon ':' need a white-space!
72
73 Allow forwarding and configure firewall
74 ************
75
76 In order to forward traffic to hosts behind the gateway the following
77 option has to be enabled on your host:
78
79         sysctl net.ipv4.ip_forward=1
80         sysctl net.ipv6.conf.all.forwarding=1
81
82 This can be added to /etc/sysctl.conf to enable it permanently.
83
84 Makes sure the ports accept traffic and masquerading:
85         sudo iptables -A INPUT -p udp -dport 500/4500 -j ACCEPT
86         sudo iptables -t nat -A POSTROUTING -s 172.16.16.0/24 -o eth0 -j MASQUERADE
87