Unable to add shared secret for VPN (NEVPNProtocolIPSec) swift

I am developing VPN app for iOS and macOS when i am trying to connect the message that is displaying is
No VPN shared secret was provided. Verify your settings and try reconnecting.
to store password and shared key i am using keychainAccess[ https://github.com/kishikawakatsumi/KeychainAccess#requirements]. what could be the reason below block code which i am using to save preferences.




let p = NEVPNProtocolIPSec()

  p.username = "vpn"
  let ip = self.getIFAddresses()
  print(ip)
  p.serverAddress = "167.179.69.252"
  p.remoteIdentifier = "167.179.69.252"
  p.localIdentifier = "192.168.xx.xx"



  p.authenticationMethod = .sharedSecret

  do {
  try self.kcs.set("vpn", key: "sharedPsw")
  try self.kcs.set("vpn", key: "password")

  }
  catch let error {
  print(error)
  }

  p.sharedSecretReference = self.kcs[attributes: "sharedPsw"]?.persistentRef
  p.passwordReference = self.kcs[attributes: "password"]?.persistentRef

  p.useExtendedAuthentication = true
  p.disconnectOnSleep = false
  self.vpnManager.protocolConfiguration = p
  self.vpnManager.localizedDescription = "secure_VPN"
  self.vpnManager.isEnabled = true
  self.vpnManager.isOnDemandEnabled = true

  self.vpnManager.saveToPreferences { error in
  guard error == nil else {
  print("NEVPNManager.saveToPreferencesWithCompletionHandler failed: \(error!.localizedDescription)")
  return
  }
  }
  }

Replies

It’s hard to say without digging into the details of the third-party keychain wrapper you’re using. I can, however, point you at the code I used for this. Specifically, this post has my

VPNKeychain
code and I set up the shared secret as follows:
proto.sharedSecretReference = try VPNKeychain.persistentReferenceFor(service: "VPNConfiguration", account: "SharedSecret", password: sharedSecret)

Note The values I use for the

service
and
account
parameters don’t matter, other than that you have to avoid colliding with other generic password keychain items.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi,

I used VPNKeychain now its new issue it's giving alert like The VPN server did not respond. Verify the server address and try reconnecting.


these are credetials

server address : 45.32.23.55

username : vpn

password : vpn

sharedsecret : vpn


let p = NEVPNProtocolIPSec()

        p.username = "vpn"

        p.serverAddress =  "45.32.23.55"

//        p.remoteIdentifier = "45.32.23.55"

//        p.localIdentifier = "192.168.0.xx"

        p.authenticationMethod = NEVPNIKEAuthenticationMethod.sharedSecret


        do {

            let vpn: Data? = "vpn".data(using: .utf8)

            let sharedSecrectKey: Data? = "vpn".data(using: .utf8)

   

            p.sharedSecretReference = try VPNKeychain.persistentReferenceFor(service: "45.32.23.55", account: "saredVpn", password: sharedSecrectKey!)

            p.passwordReference = try VPNKeychain.persistentReferenceFor(service: "45.32.23.55", account: "paswordvpn", password: vpn!)

        } catch let error {

            print(error)

        }

        p.useExtendedAuthentication = true

        self.vpnManager.isEnabled = true


        p.disconnectOnSleep = false

        self.vpnManager.protocolConfiguration = p

        self.vpnManager.localizedDescription = "secure vpn"

        self.vpnManager.isOnDemandEnabled = true



        var rules = [NEOnDemandRule]()

        let rule = NEOnDemandRuleConnect()

        rule.interfaceTypeMatch = .any

        rules.append(rule)



        print("saving")

        assert(self.vpnManager.isEnabled)



        self.vpnManager.saveToPreferences { error in

            guard error == nil else {

                print("NEVPNManager.saveToPreferencesWithCompletionHandler failed: \(error!.localizedDescription)")

                return

            }

   



            self.vpnManager.loadFromPreferences(completionHandler: { (saveError) in


                do {

        

                    try self.vpnManager.connection.startVPNTunnel()

   

                } catch let error {

                    print("Error starting VPN Connection \(error.localizedDescription)");

                }


            })


        }