# Strongswan IKEv2 + Iphone (iOS 13.4.1)



## duk2nuk (Jun 10, 2020)

Good day, everyone!
I have recently configured StrongSwan on my FreeBSD 12.1-RELEASE, 
I don't have domain name, only white IP. I have generated certificate and key with easy-rsa with CN={my_ip}

ipsec.conf:

```
config setup
        # strictcrlpolicy=yes
        # uniqueids = no
# Add connections here.
conn ipsec-ikev2
  keyexchange=ikev2
  dpdaction=clear
  dpddelay=35s
  dpdtimeout=300s
  fragmentation=yes
  rekey=no
  mobike=yes
left=%any
  leftsubnet=0.0.0.0/0
  leftcert={my_ip}.crt
  leftsendcert=always
  leftid={my_ip}
right=%any
  rightsourceip=192.168.200.0/24
  rightdns=8.8.8.8,8.8.4.4
  rightsendcert=never
  auto=add
  eap_identity=%identity
  rightauth=eap-mschapv2
  rightid=%any
```
my ipsec.secret:

```
# ipsec.secrets - strongSwan IPsec secrets file
: RSA "{my_ip}.key"
{user} : EAP "pass"
```

On Iphone:

```
Type - IKEv2
Server: {my_ip}
Remote ID: {my_ip}
Local ID: {user}

User Authentication: Username 
Username: {user}
Password: pass
```

But there are errors when I connect with iPhone. This is the output from  `ipsec start --nofork`:

```
Starting strongSwan 5.8.3 IPsec [starter]...
00[DMN] Starting IKE charon daemon (strongSwan 5.8.3, FreeBSD 12.1-RELEASE-p5, amd64)
00[KNL] unable to set UDP_ENCAP: Invalid argument
00[NET] enabling UDP decapsulation for IPv6 on port 4500 failed
00[KNL] known interfaces and IP addresses:
00[KNL]   re0
00[KNL]     192.168.1.1
00[KNL]   re1
00[KNL]     {my_ip}
00[KNL]   lo0
00[KNL]     ::1
00[KNL]     fe80::1
00[KNL]     127.0.0.1
00[KNL]   tun0
00[KNL]     fe80::1e1b:dff:febd:5a7d
00[KNL]     10.8.0.1
00[CFG] loading ca certificates from '/usr/local/etc/ipsec.d/cacerts'
00[CFG]   loaded ca certificate "CN={my_ip}" from '/usr/local/etc/ipsec.d/cacerts/ca.crt'
00[CFG] loading aa certificates from '/usr/local/etc/ipsec.d/aacerts'
00[CFG] loading ocsp signer certificates from '/usr/local/etc/ipsec.d/ocspcerts'
00[CFG] loading attribute certificates from '/usr/local/etc/ipsec.d/acerts'
00[CFG] loading crls from '/usr/local/etc/ipsec.d/crls'
00[CFG] loading secrets from '/usr/local/etc/ipsec.secrets'
00[CFG]   loaded RSA private key from '/usr/local/etc/ipsec.d/private/{my_ip}.key'
00[CFG]   loaded EAP secret for {user}
00[LIB] loaded plugins: charon aes des blowfish rc2 sha2 sha1 md4 md5 random nonce x509 revocation constraints pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey pem openssl fips-prf curve25519 xcbc cmac hmac drbg curl attr kernel-pfkey kernel-pfroute resolve socket-default stroke vici updown eap-identity eap-md5 eap-mschapv2 eap-tls eap-ttls eap-peap xauth-generic whitelist addrblock counters
00[JOB] spawning 16 worker threads
03[NET] waiting for data on sockets
charon (9680) started after 80 ms
01[CFG] received stroke: add connection 'ipsec-ikev2'
01[CFG] conn ipsec-ikev2
01[CFG]   left=%any
01[CFG]   leftsubnet=0.0.0.0/0
01[CFG]   leftid={my_ip}
01[CFG]   leftcert={my_ip}.crt
01[CFG]   right=%any
01[CFG]   rightsourceip=192.168.200.0/24
01[CFG]   rightdns=8.8.8.8,8.8.4.4
01[CFG]   rightauth=eap-mschapv2
01[CFG]   rightid=%any
01[CFG]   eap_identity=%identity
01[CFG]   dpddelay=35
01[CFG]   dpdtimeout=300
01[CFG]   dpdaction=1
01[CFG]   sha256_96=no
01[CFG]   mediation=no
01[CFG]   keyexchange=ikev2
01[CFG] adding virtual IP address pool 192.168.200.0/24
01[CFG]   loaded certificate "CN={my_ip}" from '{my_ip}.crt'
01[CFG]   id '{my_ip}' not confirmed by certificate, defaulting to 'CN={my_ip}'
01[CFG] added configuration 'ipsec-ikev2'
03[NET] received packet => 604 bytes @ 0x00007fffdfbf95e0
03[NET] received packet: from 31.173.84.211[4081] to {my_ip}[500]
03[NET] waiting for data on sockets
01[MGR] checkout IKEv2 SA by message with SPIs f26904172cbf8da7_i 0000000000000000_r
01[MGR] created IKE_SA (unnamed)[1]
01[NET] <1> received packet: from 31.173.84.211[4081] to {my_ip}[500] (604 bytes)
01[ENC] <1> parsed IKE_SA_INIT request 0 [ SA KE No N(REDIR_SUP) N(NATD_S_IP) N(NATD_D_IP) N(FRAG_SUP) ]
01[CFG] <1> looking for an IKEv2 config for {my_ip}...31.173.84.211
01[CFG] <1> ike config match: 28 (%any...%any IKEv2)
01[CFG] <1>   candidate: %any...%any, prio 28
01[CFG] <1> found matching ike config: %any...%any with prio 28
01[IKE] <1> 31.173.84.211 is initiating an IKE_SA
01[IKE] <1> IKE_SA (unnamed)[1] state change: CREATED => CONNECTING
01[CFG] <1> selecting proposal:
01[CFG] <1>   proposal matches
01[CFG] <1> received proposals: IKE:AES_CBC_256/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_2048, IKE:AES_CBC_256/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/ECP_256, IKE:AES_CBC_256/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_1536, IKE:AES_CBC_128/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_1024, IKE:3DES_CBC/HMAC_SHA1_96/PRF_HMAC_SHA1/MODP_1024
01[CFG] <1> configured proposals: IKE:AES_CBC_128/AES_CBC_192/AES_CBC_256/CAMELLIA_CBC_128/CAMELLIA_CBC_192/CAMELLIA_CBC_256/3DES_CBC/HMAC_SHA2_256_128/HMAC_SHA2_384_192/HMAC_SHA2_512_256/HMAC_SHA1_96/AES_XCBC_96/AES_CMAC_96/PRF_HMAC_SHA2_256/PRF_HMAC_SHA2_384/PRF_HMAC_SHA2_512/PRF_AES128_XCBC/PRF_AES128_CMAC/PRF_HMAC_SHA1/ECP_256/ECP_384/ECP_521/ECP_256_BP/ECP_384_BP/ECP_512_BP/CURVE_25519/CURVE_448/MODP_3072/MODP_4096/MODP_6144/MODP_8192/MODP_2048, IKE:AES_GCM_16_128/AES_GCM_16_192/AES_GCM_16_256/CHACHA20_POLY1305/AES_GCM_12_128/AES_GCM_12_192/AES_GCM_12_256/AES_GCM_8_128/AES_GCM_8_192/AES_GCM_8_256/PRF_HMAC_SHA2_256/PRF_HMAC_SHA2_384/PRF_HMAC_SHA2_512/PRF_AES128_XCBC/PRF_AES128_CMAC/PRF_HMAC_SHA1/ECP_256/ECP_384/ECP_521/ECP_256_BP/ECP_384_BP/ECP_512_BP/CURVE_25519/CURVE_448/MODP_3072/MODP_4096/MODP_6144/MODP_8192/MODP_2048
01[CFG] <1> selected proposal: IKE:AES_CBC_256/HMAC_SHA2_256_128/PRF_HMAC_SHA2_256/MODP_2048
01[IKE] <1> natd_chunk => 22 bytes @ 0x00000008022109c0
01[IKE] <1>    0: F2 69 04 17 2C BF 8D A7 00 00 00 00 00 00 00 00  .i..,...........
01[IKE] <1>   16: 4D 25 CC 4C 01 F4                                M%.L..
01[IKE] <1> natd_hash => 20 bytes @ 0x0000000802210980
01[IKE] <1>    0: 1D 5D AA F0 57 11 E5 D2 D3 18 C7 EB 7F C4 89 49  .]..W..........I
01[IKE] <1>   16: B9 5B C1 41                                      .[.A
01[IKE] <1> natd_chunk => 22 bytes @ 0x00000008022109c0
01[IKE] <1>    0: F2 69 04 17 2C BF 8D A7 00 00 00 00 00 00 00 00  .i..,...........
01[IKE] <1>   16: 1F AD 54 D3 0F F1                                ..T...
01[IKE] <1> natd_hash => 20 bytes @ 0x00000008022109e0
01[IKE] <1>    0: 07 05 31 05 F0 D9 5A 31 48 38 22 57 11 55 8B 24  ..1...Z1H8"W.U.$
01[IKE] <1>   16: 2D B3 B7 64                                      -..d
01[IKE] <1> precalculated src_hash => 20 bytes @ 0x00000008022109e0
01[IKE] <1>    0: 07 05 31 05 F0 D9 5A 31 48 38 22 57 11 55 8B 24  ..1...Z1H8"W.U.$
01[IKE] <1>   16: 2D B3 B7 64                                      -..d
01[IKE] <1> precalculated dst_hash => 20 bytes @ 0x0000000802210980
01[IKE] <1>    0: 1D 5D AA F0 57 11 E5 D2 D3 18 C7 EB 7F C4 89 49  .]..W..........I
01[IKE] <1>   16: B9 5B C1 41                                      .[.A
01[IKE] <1> received src_hash => 20 bytes @ 0x00000008022107e0
01[IKE] <1>    0: 2A 4B 90 5A 2F 96 6B 80 10 F0 5D A4 2B 60 5E 31  *K.Z/.k...].+`^1
01[IKE] <1>   16: 31 0D CC 57                                      1..W
01[IKE] <1> received dst_hash => 20 bytes @ 0x0000000802210820
01[IKE] <1>    0: 1D 5D AA F0 57 11 E5 D2 D3 18 C7 EB 7F C4 89 49  .]..W..........I
01[IKE] <1>   16: B9 5B C1 41                                      .[.A
01[IKE] <1> remote host is behind NAT
01[IKE] <1> natd_chunk => 22 bytes @ 0x00000008022108c0
01[IKE] <1>    0: F2 69 04 17 2C BF 8D A7 E3 04 8E BA 39 C2 71 D3  .i..,.......9.q.
01[IKE] <1>   16: 4D 25 CC 4C 01 F4                                M%.L..
01[IKE] <1> natd_hash => 20 bytes @ 0x00000008022108a0
01[IKE] <1>    0: 9E 0C DD D4 ED 40 AD C1 DA 6E 88 AC 0F 9C 67 B1  .....@...n....g.
01[IKE] <1>   16: CA A9 B2 CE                                      ....
01[IKE] <1> natd_chunk => 22 bytes @ 0x0000000802210880
01[IKE] <1>    0: F2 69 04 17 2C BF 8D A7 E3 04 8E BA 39 C2 71 D3  .i..,.......9.q.
01[IKE] <1>   16: 1F AD 54 D3 0F F1                                ..T...
01[IKE] <1> natd_hash => 20 bytes @ 0x0000000802210960
01[IKE] <1>    0: 66 38 D7 BE 5D D4 EE EF FE 89 CB 61 40 70 74 E4  f8..]......a@pt.
01[IKE] <1>   16: C7 F3 FC C6                                      ....
01[ENC] <1> generating IKE_SA_INIT response 0 [ SA KE No N(NATD_S_IP) N(NATD_D_IP) N(FRAG_SUP) N(CHDLESS_SUP) N(MULT_AUTH) ]
01[NET] <1> sending packet: from {my_ip}[500] to 31.173.84.211[4081] (456 bytes)
01[MGR] <1> checkin IKE_SA (unnamed)[1]
01[MGR] <1> checkin of IKE_SA successful
04[NET] sending packet: from {my_ip}[500] to 31.173.84.211[4081]
03[NET] received packet => 500 bytes @ 0x00007fffdfbf95e0
03[NET] received packet: from 31.173.84.211[41300] to {my_ip}[4500]
03[NET] waiting for data on sockets
01[MGR] checkout IKEv2 SA by message with SPIs f26904172cbf8da7_i e3048eba39c271d3_r
01[MGR] IKE_SA (unnamed)[1] successfully checked out
01[NET] <1> received packet: from 31.173.84.211[41300] to {my_ip}[4500] (496 bytes)
01[ENC] <1> unknown attribute type INTERNAL_DNS_DOMAIN
01[ENC] <1> parsed IKE_AUTH request 1 [ IDi N(INIT_CONTACT) IDr CPRQ(ADDR MASK DHCP DNS ADDR6 DHCP6 DNS6 DOMAIN) N(ESP_TFC_PAD_N) N(NON_FIRST_FRAG) SA TSi TSr N(MOBIKE_SUP) ]
01[CFG] <1> looking for peer configs matching {my_ip}[{my_ip}]...31.173.84.211[{user}]
01[CFG] <1> peer config "ipsec-ikev2", ike match: 28 (%any...%any IKEv2)
01[CFG] <1>   local id match: 0 (ID_IPV4_ADDR: {my_ip but in hex format})
01[CFG] <1> no matching peer config found
01[IKE] <1> processing INTERNAL_IP4_ADDRESS attribute
01[IKE] <1> processing INTERNAL_IP4_NETMASK attribute
01[IKE] <1> processing INTERNAL_IP4_DHCP attribute
01[IKE] <1> processing INTERNAL_IP4_DNS attribute
01[IKE] <1> processing INTERNAL_IP6_ADDRESS attribute
01[IKE] <1> processing INTERNAL_IP6_DHCP attribute
01[IKE] <1> processing INTERNAL_IP6_DNS attribute
01[IKE] <1> processing INTERNAL_DNS_DOMAIN attribute
01[IKE] <1> received ESP_TFC_PADDING_NOT_SUPPORTED, not using ESPv3 TFC padding
01[IKE] <1> peer supports MOBIKE
01[ENC] <1> generating IKE_AUTH response 1 [ N(AUTH_FAILED) ]
01[NET] <1> sending packet: from {my_ip}[4500] to 31.173.84.211[41300] (80 bytes)
01[MGR] <1> checkin and destroy IKE_SA (unnamed)[1]
04[NET] sending packet: from {my_ip}[4500] to 31.173.84.211[41300]
01[IKE] <1> IKE_SA (unnamed)[1] state change: CONNECTING => DESTROYING
01[MGR] checkin and destroy of IKE_SA successful
```
What am I doing wrong?


----------



## ecdsa (Jun 11, 2020)

Your certificate does not contain the IP address as subjectAlternativeName (SAN) extension so you can't use `leftid={my_ip}`:


```
01[CFG]   id '{my_ip}' not confirmed by certificate, defaulting to 'CN={my_ip}'
```

As you can see, the local identity will default to the certificate's subject distinguished name. Therefore, when the client later connects and sends the IP address as remote identity, it won't match the local identity:


```
01[CFG] <1> looking for peer configs matching {my_ip}[{my_ip}]...31.173.84.211[{user}]
01[CFG] <1> peer config "ipsec-ikev2", ike match: 28 (%any...%any IKEv2)
01[CFG] <1>   local id match: 0 (ID_IPV4_ADDR: {my_ip but in hex format})
01[CFG] <1> no matching peer config found
```

To fix this, add the IP address as SAN to the certificate (alternatively, clients could use the subject DN as remote identity, but AFAIK iOS clients still can't do that).


----------



## duk2nuk (Jun 13, 2020)

Thank! I have reconfigure all with swanctl.conf and today connection is established, but I have some troubles.

There are two gateways on FreeBSD:
`network1<=>[192.168.0.0/24]FreeBSD1[dynamic ip]==Internet==[white_ip]FreeBSD2[192.168.1.0/24]<=>network2`
FreeBSD1 doesn't have public ip, FreeBSD2 has an public IP. Network1 and Network2 are behind NAT. FreeBSD doesn't have any domain name only IP.
I have configured IPsec IKEv2 site-to site (like this https://www.strongswan.org/testing/testresults/swanctl/net2net-cert) connection between FreeBSD1 and FreeBSD2

In my situation FreBSD1 can't ping FreeBSD2 and also can't e.g ssh.
But network1 can ping FreeBSD2 and host from network2 and network2 can ping FreeBSD1 and hosts from network1.

Below are my config file.
*FreeBSD1* swanctl.conf (domain name and emails are fake):
`connections {
  gw-gw {
  local_addrs = 192.168.0.1
  remote_addrs = white_ip

  local {
    auth = pubkey
    cert {
    module = freebsd_tom_all.p12
    file =/usr/local/etc/swanctl/x509/freebsd_tom_cert.pem
    }
    id=freebsd@tomilino.org
    }
    remote {
    auth = pubkey
    id = %any
    }
    children {
      net-net {
        remote_ts = 192.168.1.0/24
        local_ts = 192.168.0.1/24
        updown = /usr/local/libexec/ipsec/_updown
        esp_proposals = aes128gcm128-x25519
        }
       }
       version = 2
       mobike = no
       proposals = aes128-sha256-x25519
     }
  }`
*FreeBSD2* swanctl.conf:
`connections {
    gw-gw {
        local_addrs = white_ip

        local {
            auth=pubkey
            certs = white_ip.pem
            id = %any
        }
        remote {
            auth = pubkey
        }
        children {
            net-net {
                local_ts = 192.168.1.0/24
                remote_ts = 192.168.0.1/24
                updown = /usr/local/libexec/ipsec/_updown
                esp_proposals = aes128gcm128-x25519,aes256-sha256-modp2048
            }
        }
        version = 2
        mobike = no
        proposals = aes128-sha256-x25519,aes256-sha256-modp2048
    }
}`
In my pf.con I have this line:
`nat on $ext_if from !($ext_if) -> ($ext_if:0)`

But updown script describes only command for iptables, not for pf! 
What I am doing wrong and how can I describe the ipsec policy? 
And I want to tunnel all traffic from FreeBSD1 inside IPsec tunnel via FreeBSD2, how can I do this?


----------



## mad_4u (Apr 3, 2022)

try Brooog IKEv2 app on ios








						‎Brooog IKEv2
					

‎IKEv2 VPN Client works on Strongswan,   You can use your own strongswan VPN server with our app, all you need is the right configuration on your server using eap-mschapv2.  Simple add VPN connection, use your servername / ip address, username, and password. our app will handle the rest and...



					apps.apple.com
				



you need to setup your server to use eap-mschapv2


----------

