5

I am making a VPN connection that requires the certificate to authentication.

The code below is how i set the configuration that VPN requires. The parameter identityData is where i put my certificate as Data.

func setupVPN(){
guard let vpnManager = NEVPNManager.shared() else { return }

vpnManager.loadFromPreferences { error in

    var hasProtocolConfig = false;

    if #available(iOS 9, *) {
        hasProtocolConfig = self.vpnManager.protocolConfiguration != nil
    } else {
        hasProtocolConfig = self.vpnManager.`protocol` != nil
    }

    if hasProtocolConfig == true {

        let p = NEVPNProtocolIKEv2()
        // All preferences here
        if let vpnData = self.vpnData {
        p.serverAddress = vpnData.getePDGAddress() // "X.X.X.X"
        p.localIdentifier = vpnData.getlocalIdentifier() // "XXXYYYZZWWWWWWWWWW@pppp.ppp.pppppp.pppppp.ppppppppppp.org"
        p.remoteIdentifier = vpnData.getAPN() // "gggggg.uuuuuuuuuuu"
        p.identityData = vpnData.getUserCertificateData() // User Certificate as Data
        }

        p.ikeSecurityAssociationParameters.integrityAlgorithm = NEVPNIKEv2IntegrityAlgorithm.SHA256
        p.ikeSecurityAssociationParameters.encryptionAlgorithm = NEVPNIKEv2EncryptionAlgorithm.algorithmAES128
        p.ikeSecurityAssociationParameters.diffieHellmanGroup = NEVPNIKEv2DiffieHellmanGroup.group14
        p.serverCertificateIssuerCommonName = "TEST SubCA"
        p.serverCertificateCommonName = "TEST SubCA"
        p.authenticationMethod = NEVPNIKEAuthenticationMethod.certificate

        if #available(iOS 9, *) {
            self.vpnManager.protocolConfiguration = p
        } else {
            self.vpnManager.`protocol` = p
        }
        self.vpnManager.isEnabled = true

        self.vpnManager.saveToPreferences { error in
            if let e = error{
                print("[VPN] error saving: " + e.localizedDescription)
            } else {
                print("[VPN] vpn saved")
                Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.connectVPN), userInfo: nil, repeats: false)
            }
            return
        }
    }
}

}

One example of that certificate encoded in base 64:

MIIFqTCCA5GgAwIBAgIQKLf5dlFRabt3cAe9ax2kXjANBgkqhkiG9w0BAQsFADBgMRwwGgYDVQQDDBNURVNUIFZGQ1ogRVBDIFN1Yk ... wdWJsaWMgYS5zLjELMAkGA1UEBhMCQ1owggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDNgTmc6uQ9Md

And then the parse to Data is done that way:

CaCertificateData = Data(base64Encoded: "Base64StringEncoded_Here")

When all set, i start the VPN tunnel that way:

do {
 try vpnManager.connection.startVPNTunnel()
} catch let error {
     print("Error starting VPN Connection \(error.localizedDescription)");
}

I can see the status of VPN and VPN starts Connecting and then becomes Disconnected. The 3 algorithm that we can see above are correct.

Someone can notice what i am doing wrong? I have some .pcap files from some different tests I have made. In all .pcap files I don't send the message "Client Hello" that is required. I think the problem is with certificate.

Pincha
  • 93
  • 1
  • 10
  • Hey did you got any solution for it ? How to connect using certificate authentication ? Coz I'm able to connect with username password approach but not with certificate. Could you post your ans. it will be helpful for others as well. Thanks. – Shrikant K Dec 22 '17 at 09:16
  • Hi did you find any solution. I am facing same problem. Thanks – Yogendra Singh Aug 03 '18 at 13:59
  • Sorry, but no. In my case was the client VPN that doesn't have support for iOS, they figure out some time later... – Pincha Dec 04 '18 at 14:24
  • @ShrikantK how did u do that? – jarvis12 Mar 19 '19 at 13:18

1 Answers1

0

you can use .ovpn files. You can easily integrate certificates inside ovpn file. Look this article https://medium.com/better-programming/how-to-build-an-openvpn-client-on-ios-c8f927c11e80

swift2geek
  • 1,697
  • 1
  • 20
  • 27
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – M-- Mar 19 '20 at 23:18