{"id":1231,"date":"2016-08-28T13:54:29","date_gmt":"2016-08-28T12:54:29","guid":{"rendered":"http:\/\/cdblog.cdstealer.com\/?p=1231"},"modified":"2019-12-09T18:32:55","modified_gmt":"2019-12-09T18:32:55","slug":"android-vpn","status":"publish","type":"post","link":"https:\/\/cdblog.cdstealer.com\/?p=1231","title":{"rendered":"Android VPN"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1426 alignnone\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/strongswanandroid.png\" alt=\"strongswanandroid\" width=\"600\" height=\"300\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/strongswanandroid.png 600w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/strongswanandroid-300x150.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>This setup below is what suited my requirements. \u00a0I'm sure there'll be another way of implementing this, but this works for me :)<\/p>\n<p>For this I'm using the following:<\/p>\n<pre># emerge --info\nPortage 2.3.0 (python 3.4.3-final-0, default\/linux\/amd64\/13.0\/no-multilib, gcc-4.9.3, glibc-2.22-r4, 4.8.7-gentoo x86_64)\n=================================================================\nSystem uname: Linux-4.8.7-gentoo-x86_64-Intel-R-_Core-TM-_i7-6700K_CPU_@_4.00GHz-with-gentoo-2.2\nKiB Mem: 32896588 total, 15127076 free\nKiB Swap: 3640916 total, 3640916 free\nTimestamp of repository gentoo: Sun, 13 Nov 2016 04:15:01 +0000<\/pre>\n<p>And this version of strongswan:<\/p>\n<pre># emerge -p strongswan\n\nThese are the packages that would be merged, in order:\n\nCalculating dependencies... done!\n[ebuild R ~] net-misc\/strongswan-5.5.0<\/pre>\n<h3>Strongswan Config<\/h3>\n<p>Once installed, we need to setup the configs. \u00a0The main config is located at \/etc\/ipsec.conf:<\/p>\n<p>This is my file with the obvious changes ;)<\/p>\n<pre># ipsec.conf - strongSwan IPsec configuration file\n\nconfig setup\n uniqueids=never\n #charondebug=\"cfg 2, dmn 2, ike 2, net 2\"\n\nconn %default\n dpdaction=restart\n dpddelay=300s\n reauth=yes\n aggressive=no\n fragmentation=yes\n type=tunnel\n forceencaps=yes\n modeconfig=pull\n auto=add\n closeaction=clear\n compress=no\n left=my.vpn.com\n leftid=\"C=GB, O=strongSwan, CN=my.vpn.com\"\n leftsubnet=0.0.0.0\/0\n leftcert=vpnHostCert.pem\n leftsendcert=always\n leftfirewall=yes\n\nconn IPSec-Android-Strongswan\n keyexchange=ikev2\n rightauth=pubkey\n rightauth2=eap-md5\n right=%any\n rightid=\"C=GB, O=strongSwan, CN=my@email.com\"\n rightsourceip=10.10.10.199\/31\n rightsendcert=ifasked\n\n# Unable to get the native VPN working with this setup\n#conn IPSec-Android-Native\n# keyexchange=ikev1\n# rightauth=pubkey\n# rightauth2=xauth\n# right=%any\n# rightsourceip=10.10.10.199\/26\n# rightsendcert=ifasked<\/pre>\n<p>I'll explain each segment in order. \u00a0The official doc can be found <a href=\"https:\/\/wiki.strongswan.org\/projects\/strongswan\/wiki\/ConnSection\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<table>\n<tbody>\n<tr>\n<td>conn %default<\/td>\n<td>This is the default stanza. \u00a0This instructs strongswan to use anything here in addition to other connections if defined.<\/td>\n<\/tr>\n<tr>\n<td>dpdaction=restart<\/td>\n<td>Controls the use of the Dead Peer Detection.<\/td>\n<\/tr>\n<tr>\n<td>dpddelay=300s<\/td>\n<td>How often to check if the client is still connected.<\/td>\n<\/tr>\n<tr>\n<td>reauth=yes<\/td>\n<td>When re-exchanging keys whether to re-athenticate.<\/td>\n<\/tr>\n<tr>\n<td>aggressive=no<\/td>\n<td>Whether to use IKEv1 Aggressive or Main Mode (the default).<\/td>\n<\/tr>\n<tr>\n<td>fragmentation=yes<\/td>\n<td>If set to yes (the default since 5.5.1) and the peer supports it, larger IKE messages will be sent in fragments.<\/td>\n<\/tr>\n<tr>\n<td>type=tunnel<\/td>\n<td>The type of the connection.<\/td>\n<\/tr>\n<tr>\n<td>forceencaps=yes<\/td>\n<td>Force UDP encapsulation for ESP packets even if no NAT situation is detected.<\/td>\n<\/tr>\n<tr>\n<td>modeconfig=pull<\/td>\n<td>Defines which mode is used to assign a virtual IP.<\/td>\n<\/tr>\n<tr>\n<td>auto=add<\/td>\n<td>What operation, if any, should be done automatically at IPsec startup.<\/td>\n<\/tr>\n<tr>\n<td>closeaction=clear<\/td>\n<td>Defines the action to take if the remote peer unexpectedly closes.<\/td>\n<\/tr>\n<tr>\n<td>compress=no<\/td>\n<td>Whether IPComp compression of content is proposed on the connection.<\/td>\n<\/tr>\n<tr>\n<td>left=my.vpn.com<\/td>\n<td>The IP address of the participant's public-network interface.<\/td>\n<\/tr>\n<tr>\n<td>leftid=\"C=GB, O=strongSwan, CN=my.vpn.com\"<\/td>\n<td>How the <em>left|right<\/em> participant should be identified for authentication.<\/td>\n<\/tr>\n<tr>\n<td>leftsubnet=0.0.0.0\/0<\/td>\n<td>Private subnet behind the left participant.<\/td>\n<\/tr>\n<tr>\n<td>leftcert=vpnHostCert.pem<\/td>\n<td>The path to the left|right participant's X.509 certificate.<\/td>\n<\/tr>\n<tr>\n<td>leftsendcert=always<\/td>\n<td><strong>Ifasked<\/strong>, meaning that<br \/>\nthe peer must send a certificate request (CR) payload in order to get a certificate in return<\/td>\n<\/tr>\n<tr>\n<td>leftfirewall=yes<\/td>\n<td>Whether the <em>left<\/em>\u00a0participant is doing forwarding-firewalling (including masquerading)<br \/>\nusing iptables for traffic from <em>leftsubnet.<\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<td>conn IPSec-Android-Strongswan<\/td>\n<td>A connection stanza<\/td>\n<\/tr>\n<tr>\n<td>keyexchange=ikev2<\/td>\n<td>Method of key exchange.<\/td>\n<\/tr>\n<tr>\n<td>rightauth=pubkey<\/td>\n<td>Authentication method to use locally (left) or require from the remote (right) side.<\/td>\n<\/tr>\n<tr>\n<td>rightauth2=eap-md5<\/td>\n<td>Same as <em>rightauth<\/em>, but defines an additional authentication exchange.<\/td>\n<\/tr>\n<tr>\n<td>right=%any<\/td>\n<td>If <em>%any<\/em> is used for the remote endpoint it literally means any IP address.<\/td>\n<\/tr>\n<tr>\n<td>rightid=\"C=GB, O=strongSwan, CN=my@email.com\"<\/td>\n<td>How the <em>right<\/em> participant should be identified for authentication.<\/td>\n<\/tr>\n<tr>\n<td>rightsourceip=10.10.10.199\/26<\/td>\n<td>The internal source IP to use in a tunnel for the remote peer.<\/td>\n<\/tr>\n<tr>\n<td>rightsendcert=ifasked<\/td>\n<td><strong>ifasked<\/strong>, meaning that<br \/>\nthe peer must send a certificate request (CR) payload in order to get a certificate in return.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table>\n<tbody>\n<tr>\n<td>conn IPSec-Android-Native<\/td>\n<td>A connection stanza<\/td>\n<\/tr>\n<tr>\n<td>keyexchange=ikev1<\/td>\n<td>Method of key exchange.<\/td>\n<\/tr>\n<tr>\n<td>rightauth=pubkey<\/td>\n<td>Authentication method to use locally (left) or require from the remote (right) side.<\/td>\n<\/tr>\n<tr>\n<td>rightauth2=xauth<\/td>\n<td>Same as <em>rightauth<\/em>, but defines an additional authentication exchange.<\/td>\n<\/tr>\n<tr>\n<td>right=%any<\/td>\n<td>If <em>%any<\/em> is used for the remote endpoint it literally means any IP address.<\/td>\n<\/tr>\n<tr>\n<td>rightsourceip=10.10.10.200<\/td>\n<td>The internal source IP to use in a tunnel for the remote peer.<\/td>\n<\/tr>\n<tr>\n<td>rightsendcert=ifasked<\/td>\n<td><strong>ifasked<\/strong>, meaning that<br \/>\nthe peer must send a certificate request (CR) payload in order to get a certificate in return.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Next we setup a secret.<\/p>\n<pre># cat \/etc\/ipsec.secrets \n: RSA vpnHostKey.pem\n&lt;user&gt; : XAUTH \"top_secret_password\"\n&lt;user&gt; : EAP \"top_secret_password\"<\/pre>\n<p>I'll explain each line. \u00a0Full documentation can be found <a href=\"https:\/\/wiki.strongswan.org\/projects\/1\/wiki\/IpsecSecrets\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<table>\n<tbody>\n<tr>\n<td>: RSA vpnHostKey.pem<\/td>\n<td>Sets the cert to be used for authentication.<\/td>\n<\/tr>\n<tr>\n<td>&lt;user&gt; : XAUTH \"top_secret_password\"<\/td>\n<td>Sets the password for XAUTH method of authentication.<\/td>\n<\/tr>\n<tr>\n<td>&lt;user&gt; : EAP \"top_secret_password\"<\/td>\n<td>Sets the password for EAP\u00a0method of authentication.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>That's it for the strongswan config itself. Now we need to create the certificates.<\/p>\n<p>As of version 5.8.0, ipsec.conf and ipsec.secrets are no longer required or work.\u00a0 These now need to be converted into a json format in swanctl.conf.<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<pre>connections {\n        IPSec-Android-Strongswan {\n          unique=no\n          version=2\n          dpd_delay=300s\n          rekey_time=0\n          reauth_time=0\n          aggressive=no\n          fragmentation=yes\n          encap=yes\n          pull=yes\n          version=2\n          pools=vpn_example\n          mobike=yes\n          send_cert=always\n                local {\n                  certs=vpnHostCert.pem\n                  id = @vpn.example.com\n                }\n                remote {\n                  auth=pubkey\n                }\n                remote2 {\n                  auth=eap-md5\n                }\n                children {\n                        IPSec-Android-Strongswan {\n                          mode=tunnel\n                          dpd_action=clear\n                          start_action=none\n                          close_action=none\n                          ipcomp=yes\n                          local_ts=0.0.0.0\/0\n                        }\n                }\n        }\n}\npools {\n  vpn_example {\n    addrs=10.10.10.10\/30\n  }\n}\nsecrets {\n  private-vpn_example {\n    file=vpnHostKey.pem\n  }\n  eap-user1 {\n    id=username1\n    secret=\"password1\"\n  }\n  eap-user2 {\n    id=username2\n    secret=\"password2\"\n  }\n}\nauthorities {\n  IPSec-Android-Strongswan {\n    cacert=strongswanCert.pem\n  }\n}\n<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Certificates<\/h3>\n<p>NOTE:\u00a0 From 5.8.0, the certificate paths have changed from \/etc\/ipsec.d\/... to \/etc\/swanctl\/...<\/p>\n<p>x509\/ = User Certs<\/p>\n<p>private\/ = Private key(s)<\/p>\n<p>x509ca\/ = Root cert(s)<\/p>\n<p>Let's start by creating the root certificate:<\/p>\n<pre class=\"line-numbers language-bash\"><code class=\" language-bash\">$ cd <span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>\n$ ipsec pki <span class=\"token operator\">--<\/span>gen <span class=\"token operator\">--<\/span>type rsa <span class=\"token operator\">--<\/span>size <span class=\"token number\">4096<\/span> <span class=\"token operator\">--<\/span>outform pem <span class=\"token operator\">&gt; <\/span>private<span class=\"token operator\">\/<\/span>strongswanKey<span class=\"token punctuation\">.<\/span>pem\n$ chmod <span class=\"token number\">600<\/span> private<span class=\"token operator\">\/<\/span>strongswanKey<span class=\"token punctuation\">.<\/span>pem\n$ ipsec pki <span class=\"token operator\">--<\/span>self <span class=\"token operator\">--<\/span>ca <span class=\"token operator\">--<\/span>lifetime <span class=\"token number\">3650<\/span> <span class=\"token operator\">--<\/span><span class=\"token keyword\">in<\/span> private<span class=\"token operator\">\/<\/span>strongswanKey<span class=\"token punctuation\">.<\/span>pem <span class=\"token operator\">--<\/span>type rsa <span class=\"token operator\">--<\/span>dn <span class=\"token string\">\"C=GB, O=strongSwan, CN=strongSwan Root CA\"<\/span> <span class=\"token operator\">--<\/span>outform pem <span class=\"token operator\">&gt;<\/span> cacerts<span class=\"token operator\">\/<\/span>strongswanCert<span class=\"token punctuation\">.<\/span>pem<\/code><\/pre>\n<p>You can change the Distinguished Name (DN) to more relevant values for country (C), organization (O), and common name (CN), but you don\u2019t have to.<\/p>\n<p>To list the properties of your newly generated certificate, type in the following command:<\/p>\n<pre class=\" language-bash\"><code class=\" language-bash\">$ ipsec pki <span class=\"token operator\">--<\/span>print <span class=\"token operator\">--<\/span><span class=\"token keyword\">in<\/span> cacerts<span class=\"token operator\">\/<\/span>strongswanCert<span class=\"token punctuation\">.<\/span>pem<\/code><\/pre>\n<p>Create your VPN host certificate:<\/p>\n<pre>$ cd \/etc\/ipsec.d\/\n$ ipsec pki --gen --type rsa --size 2048\u00a0--outform pem\u00a0&gt; private\/vpnHostKey.pem\n$ chmod 600 private\/vpnHostKey.pem\n$ ipsec pki --pub --in private\/vpnHostKey.pem --type rsa |\u00a0ipsec pki --issue --lifetime 730\u00a0--cacert cacerts\/strongswanCert.pem\u00a0--cakey private\/strongswanKey.pem\u00a0--dn \"C=GB, O=strongSwan, CN=my.vpn.com\"\u00a0--san my.vpn.com\u00a0--flag serverAuth --flag ikeIntermediate\u00a0--outform pem &gt; certs\/vpnHostCert.pem<\/pre>\n<p>To look at the properties of your new certificate, execute the command:<\/p>\n<pre class=\" language-bash\"><code class=\" language-bash\">ipsec pki <span class=\"token operator\">--<\/span>print <span class=\"token operator\">--<\/span><span class=\"token keyword\">in<\/span> certs<span class=\"token operator\">\/<\/span>vpnHostCert<span class=\"token punctuation\">.<\/span>pem<\/code><\/pre>\n<p>Create a client certificate:<\/p>\n<pre class=\"line-numbers language-bash\"><code class=\" language-bash\">$ cd <span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>\n$ ipsec pki <span class=\"token operator\">--<\/span>gen <span class=\"token operator\">--<\/span>type rsa <span class=\"token operator\">--<\/span>size <span class=\"token number\">2048<\/span> <span class=\"token operator\">--<\/span>outform pem <span class=\"token operator\">&gt;<\/span> private<span class=\"token operator\">\/User<\/span>Key<span class=\"token punctuation\">.<\/span>pem\n$ chmod <span class=\"token number\">600<\/span> private<span class=\"token operator\">\/User<\/span>Key<span class=\"token punctuation\">.<\/span>pem\n$ ipsec pki <span class=\"token operator\">--<\/span>pub <span class=\"token operator\">--<\/span><span class=\"token keyword\">in<\/span> private<span class=\"token operator\">\/User<\/span>Key<span class=\"token punctuation\">.<\/span>pem <span class=\"token operator\">--<\/span>type rsa <span class=\"token operator\">|<\/span> ipsec pki <span class=\"token operator\">--<\/span>issue <span class=\"token operator\">--<\/span>lifetime <span class=\"token number\">730<\/span> <span class=\"token operator\">--<\/span>cacert cacerts<span class=\"token operator\">\/<\/span>strongswanCert<span class=\"token punctuation\">.<\/span>pem <span class=\"token operator\">--<\/span>cakey private<span class=\"token operator\">\/<\/span>strongswanKey<span class=\"token punctuation\">.<\/span>pem <span class=\"token operator\">--<\/span>dn <span class=\"token string\">\"C=GB, O=strongSwan, CN=user@vpn.com\"<\/span> <span class=\"token operator\">--<\/span>san user@vpn.com <span class=\"token operator\">--<\/span>outform pem <span class=\"token operator\">&gt;<\/span> certs<span class=\"token operator\">\/User<\/span>Cert<span class=\"token punctuation\">.<\/span>pem<\/code><\/pre>\n<p>Export client certificate as a\u00a0PKCS#12 file:<\/p>\n<pre>cd \/etc\/ipsec.d\/\n$ openssl pkcs12 -export -inkey private\/UserKey.pem \\\n -in certs\/UserCert.pem -name \"User's VPN Certificate\" \\\n -certfile cacerts\/strongswanCert.pem \\\n -caname \"strongSwan Root CA\" \\\n -out User.p12<\/pre>\n<p>Revoke a certificate (if needed):<\/p>\n<pre>$ cd \/etc\/ipsec.d\/\n$ ipsec pki --signcrl --reason key-compromise \\\n --cacert cacerts\/strongswanCert.pem \\\n --cakey private\/strongswanKey.pem \\\n --cert certs\/UserCert.pem \\\n --outform pem &gt; crls\/crl.pem<\/pre>\n<p>To add another revoked certificate to the same list, we need to copy the existing list into a temporary file:<\/p>\n<pre class=\"line-numbers language-bash\"><code class=\" language-bash\">$ cd <span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>\n$ cp crls<span class=\"token operator\">\/<\/span>crl<span class=\"token punctuation\">.<\/span>pem crl<span class=\"token punctuation\">.<\/span>pem<span class=\"token punctuation\">.<\/span>tmp\n$ ipsec pki <span class=\"token operator\">--<\/span>signcrl <span class=\"token operator\">--<\/span>reason key<span class=\"token operator\">-<\/span>compromise \\\n\t<span class=\"token operator\">--<\/span>cacert cacerts<span class=\"token operator\">\/<\/span>strongswanCert<span class=\"token punctuation\">.<\/span>pem \\\n\t<span class=\"token operator\">--<\/span>cakey private<span class=\"token operator\">\/<\/span>strongswanKey<span class=\"token punctuation\">.<\/span>pem \\\n\t<span class=\"token operator\">--<\/span>cert certs<span class=\"token operator\">\/<\/span>AnotherStolenCert<span class=\"token punctuation\">.<\/span>pem \\\n\t<span class=\"token operator\">--<\/span>lastcrl crl<span class=\"token punctuation\">.<\/span>pem<span class=\"token punctuation\">.<\/span>tmp \\\n\t<span class=\"token operator\">--<\/span>outform pem <span class=\"token operator\">&gt;<\/span> crls<span class=\"token operator\">\/<\/span>crl<span class=\"token punctuation\">.<\/span>pem\n$ rm crl<span class=\"token punctuation\">.<\/span>pem<span class=\"token punctuation\">.<\/span>tmp<\/code><\/pre>\n<p>CERTIFICATES \u2013 RECAP:<\/p>\n<p>So far you\u2019ve created the following files:<\/p>\n<pre class=\" language-bash\"><code class=\" language-bash\"><span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>private<span class=\"token operator\">\/<\/span>strongswanKey<span class=\"token punctuation\">.<\/span>pem  <span class=\"token comment\" spellcheck=\"true\"># CA private key\n<\/span><span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>cacerts<span class=\"token operator\">\/<\/span>strongswanCert<span class=\"token punctuation\">.<\/span>pem <span class=\"token comment\" spellcheck=\"true\"># CA certificate\n<\/span><span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>private<span class=\"token operator\">\/<\/span>vpnHostKey<span class=\"token punctuation\">.<\/span>pem     <span class=\"token comment\" spellcheck=\"true\"># VPN host private key\n<\/span><span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>certs<span class=\"token operator\">\/<\/span>vpnHostCert<span class=\"token punctuation\">.<\/span>pem      <span class=\"token comment\" spellcheck=\"true\"># VPN host certificate\n<\/span><span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>private<span class=\"token operator\">\/Us<\/span>erKey<span class=\"token punctuation\">.<\/span>pem   <span class=\"token comment\" spellcheck=\"true\"># Client \"User\" private key\n<\/span><span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/<\/span>certs<span class=\"token operator\">\/Us<\/span>erCert<span class=\"token punctuation\">.<\/span>pem    <span class=\"token comment\" spellcheck=\"true\"># Client \"User\" certificate\n<\/span><span class=\"token operator\">\/<\/span>etc<span class=\"token operator\">\/<\/span>ipsec<span class=\"token punctuation\">.<\/span>d<span class=\"token operator\">\/Us<\/span>er<span class=\"token punctuation\">.<\/span>p12              <span class=\"token comment\" spellcheck=\"true\"># Client \"User\" PKCS#12 file<\/span><\/code><\/pre>\n<h3>Firewall Rules<\/h3>\n<p>I'm sure you'll have some sort of router ;) \u00a0So you'll need to open up and forward the ports to your VPN server.<\/p>\n<p>You'll only need UDP ports 500 &amp; 4500.<\/p>\n<p>Port 500 is the IPSEC port and 4500 is the port used for NATing.<\/p>\n<p>That's the entry point sorted. \u00a0Now for iptables.<\/p>\n<p>I have these defined to manage the VPN traffic:<\/p>\n<pre>iptables -t nat -A POSTROUTING -s &lt;VIP&gt; -o &lt;INTERFACE&gt; -m policy --dir out --pol ipsec -j ACCEPT\niptables -t nat -A POSTROUTING -s &lt;VIP&gt; -o &lt;INTERFACE&gt; -j MASQUERADE\niptables -t nat -A POSTROUTING -o &lt;INTERFACE&gt; ! -p esp -j SNAT --to-source &lt;VPN_IP&gt;\niptables -A INPUT -p esp -j ACCEPT\niptables -A INPUT -p ah -j ACCEPT<\/pre>\n<p>&lt;VIP&gt; is the IP\/CIDR which is assigned to the client.<br \/>\n&lt;INTERFACE&gt; is the network interface name eg <strong>eth0.<br \/>\n<\/strong>&lt;VPN_IP&gt; is the IP the VPN server listens on.<br \/>\nThe bottom two just accept the required protocols.<\/p>\n<p>You may also need to open up access to ranges used by your mobile service provider. \u00a0eg<\/p>\n<pre>iptables -A INPUT -s &lt;mobile_cidr&gt;\/22 -p udp -m multiport --dports 500,4500 -j ACCEPT<\/pre>\n<p>You may also need to allow the VIP address assigned to the client to connect to internal services. \u00a0This could be something as global as:<\/p>\n<pre>iptables -A INPUT -s 10.10.10.200 -j ACCEPT<\/pre>\n<p>or could be tied down to specific ports.<\/p>\n<h3>DNS<\/h3>\n<p>If the virtual IP that is assigned is on the same network as the server, you can just add the following to your ipsec.conf:<\/p>\n<pre>rightdns = &lt;dns_server_ip&gt;<\/pre>\n<p>If you assign a virtual IP that is on a different network, then you will need to make some additional changes. \u00a0You won't see any errors regarding DNS in any logs, it just won't work!<br \/>\nFor example, in the client log, you'd see:<\/p>\n<pre>Aug 28 06:59:18 08[IKE] installing DNS server 111.111.111.119\nAug 28 06:59:18 08[IKE] installing new virtual IP 112.112.112.120\n<\/pre>\n<p>but if you try and resolve any internal hostnames, it just won't work.<\/p>\n<p>You need to make the following change to either strongswan.conf or charon.conf (recommended) within the strongswan.d directory.<\/p>\n<pre>charon {\n<span style=\"color: #ff0000;\"><strong>    plugins {<\/strong><\/span>\n<span style=\"color: #ff0000;\"><strong>        attr {<\/strong><\/span>\n<span style=\"color: #ff0000;\"><strong>        dns = 111.111.111.119<\/strong><\/span>\n<span style=\"color: #ff0000;\"><strong>       split-include = 112.112.112.120, 111.111.111.0\/24<\/strong><\/span>\n<span style=\"color: #ff0000;\"><strong>             }<\/strong><\/span>\n<span style=\"color: #ff0000;\"><strong>            }<\/strong><\/span>\n       }<\/pre>\n<p>As we are splitting the DNS, we need to use the <strong>attr<\/strong> plugin. \u00a0You can check if this is loaded when the strongswan daemon is started.<\/p>\n<pre>Aug 28 07:28:23 &lt;hostname&gt; charon[22120]: 00[LIB] loaded plugins: charon pkcs11 aes des blowfish rc2 sha2 sha1 md4 md5 rdrand random nonce x509 revocation constraints pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey pem openssl gcrypt fips-prf gmp xcbc cmac hmac ctr ccm gcm <span style=\"color: #ff0000;\"><strong>attr<\/strong><\/span> kernel-netlink resolve socket-default socket-dynamic farp stroke vici updown eap-identity eap-sim eap-aka eap-aka-3gpp2 eap-simaka-pseudonym eap-simaka-reauth eap-md5 eap-gtc eap-mschapv2 eap-radius eap-tls xauth-generic xauth-eap xauth-pam dhcp unity<\/pre>\n<p>The other thing you will need to do (only if you run your own DNS) is to allow the VPN network to query DNS. \u00a0This is done by adding the VPN subnet or assigned virtual IP of the client to named.conf.<\/p>\n<p>This is found within the options block.<\/p>\n<pre>allow-query { 127.0.0.1; 111.111.111.0\/24; <strong><span style=\"color: #ff0000;\">112.112.112.120;<\/span><\/strong> };<\/pre>\n<h2>Client setup (<a href=\"https:\/\/play.google.com\/store\/apps\/details?id=org.strongswan.android\" target=\"_blank\" rel=\"noopener noreferrer\">strongswan<\/a>)<\/h2>\n<p>First of all, you'll need to get the pk12 certificate you've created onto your phone (email, cloud storage etc etc). \u00a0Once saved to a location, we need to load it into Android.<\/p>\n<p>Go to settings and tap \"Security\".<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1762 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105524-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105524-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105524-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105524-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105524.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>Then tap \"Install from storage\".<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1764 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105752-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105752-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105752-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105752-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105752.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>Browse to where you saved the cert. \u00a0You will be asked for the passphrase set in the cert.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1765 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114240-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114240-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114240-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114240-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114240.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>Once installed, go back to \"Security\" and tap \"User credentials\"<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1766 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114733-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114733-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114733-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114733-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-114733.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>You should see your certificate entry. \u00a0There are no details to be had here. \u00a0The only option if you tap the cert is to remove it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1767 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105739-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105739-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105739-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105739-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105739.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>That's it for the certificate installation; now onto the client setup.<\/p>\n<p>If you haven't installed Strongswan, what are you waiting for? ;)<\/p>\n<p>Open the app and tap \"ADD VPN PROFILE\"<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1768 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115315-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115315-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115315-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115315-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115315.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>Complete the details and ensure you have selected the installed certificate (User certificate).<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1769 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105930-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105930-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105930-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105930-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-105930.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n<p>Click SAVE and you're done.<\/p>\n<p>Now to test it! \u00a0Disconnect from your wifi and tap the entry you should now have in the strongswan app. \u00a0If all went well, you should be connected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1770 size-large\" src=\"http:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115957-576x1024.png\" alt=\"\" width=\"474\" height=\"843\" srcset=\"https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115957-576x1024.png 576w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115957-169x300.png 169w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115957-300x533.png 300w, https:\/\/cdblog.cdstealer.com\/wp-content\/uploads\/2016\/08\/Screenshot_20170625-115957.png 1440w\" sizes=\"auto, (max-width: 474px) 100vw, 474px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This setup below is what suited my requirements. \u00a0I'm sure there'll be another way of implementing this, but this works for me :) For this I'm using the following: # emerge --info Portage 2.3.0 (python 3.4.3-final-0, default\/linux\/amd64\/13.0\/no-multilib, gcc-4.9.3, glibc-2.22-r4, 4.8.7-gentoo x86_64) ================================================================= System uname: Linux-4.8.7-gentoo-x86_64-Intel-R-_Core-TM-_i7-6700K_CPU_@_4.00GHz-with-gentoo-2.2 KiB Mem: 32896588 total, 15127076 free KiB Swap: 3640916 total, &hellip; <a href=\"https:\/\/cdblog.cdstealer.com\/?p=1231\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Android VPN<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[161,18,251,252],"class_list":["post-1231","post","type-post","status-publish","format-standard","hentry","category-gentoo","tag-android-2","tag-linux","tag-strongswan","tag-vpn"],"_links":{"self":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1231"}],"version-history":[{"count":33,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1231\/revisions"}],"predecessor-version":[{"id":2040,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=\/wp\/v2\/posts\/1231\/revisions\/2040"}],"wp:attachment":[{"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cdblog.cdstealer.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}