Need help with VPN IPsec

Hi Dears

I need your kindly help to make vpn ipsec programmatically for the IOS ..... I have the crediantials for IPsec VPN below :


1- Server IP : this is server IP

2- Account : each user has his user name

3-Password : each user has his password

4- Group Name : this is share with all users

5- Secret : this is share with all users


Thanks for your helping in advance


Regards

Khaled

Replies

Dears Please any replay ??

You should look at the Personal VPN feature of NEVPNManager, which lets you programmatically configure IPsec VPN (subject to the user’s approval, of course).

Share and Enjoy

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

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

Hi Dear

thank you for the replay but is there any examples about that ??? becuse i didnt find the group name

Please i need answer for this quastion


Regards

Khaled Abdullah

is there any examples about that ?

There’s no official sample code for this but I expect you’ll find a bunch of unofficial code for it out there on the ’net.

I generally recommend that you start this process by creating a configuration profile for your VPN setup. You can do this using Apple Configurator. Once you have a configuration profile up and running, it’s a relatively straightforward job converting that to NEVPNManager code. If you get stuck with that part, I’d be happy to help.

Share and Enjoy

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

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

Thanks for your replay eskimo

I have the VPN profile working in my phone .

i will try with apple configrator and revent back to you


Best Regards

Khaled

Hi eskimo

In the application it is every time i got on the pop-up message ask me for the user's password

please your help to solve this issue


Regards

Khlaed

Dears

please any one help me with this


Regards

Khaled

In the application it is every time i got on the pop-up message ask me for the user's password

That’s usually caused by an authentication failure, meaning you haven’t set up the user’s credentials properly. Make sure you:

  • Set

    useExtendedAuthentication
    to true
  • Set

    username
    to the account name
  • Set

    passwordReference
    to reference the user’s password in your keychain

This last part is a bit tricky. Pasted in below is the code I use for it.

Share and Enjoy

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

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

/// Utility routines for working with the keychain.

enum VPNKeychain {

    /// Returns a persistent reference for a generic password keychain item, adding it to 
    /// (or updating it in) the keychain if necessary.
    ///
    /// This delegates the work to two helper routines depending on whether the item already 
    /// exists in the keychain or not.
    ///
    /// - Parameters:
    ///   - service: The service name for the item.
    ///   - account: The account for the item.
    ///   - password: The desired password.
    /// - Returns: A persistent reference to the item.
    /// - Throws: Any error returned by the Security framework.

    static func persistentReferenceFor(service: String, account: String, password: Data) throws -> Data {
        var copyResult: CFTypeRef? = nil
        let err = SecItemCopyMatching([
            kSecClass: kSecClassGenericPassword, 
            kSecAttrService: service, 
            kSecAttrAccount: account, 
            kSecReturnPersistentRef: true, 
            kSecReturnData: true 
        ] as NSDictionary, &copyResult)
        switch err {
            case errSecSuccess:
                return try self.persistentReferenceByUpdating(copyResult: copyResult!, service: service, account: account, password: password)
            case errSecItemNotFound:
                return try self.persistentReferenceByAdding(service: service, account:account, password: password)
            default:
                try throwOSStatus(err)
                // `throwOSStatus(_:)` only returns in the `errSecSuccess` case.  We know we're 
                // not in that case but the compiler can't figure that out, alas.
                fatalError()
        }
    }

    /// Returns a persistent reference for a generic password keychain item by updating it 
    /// in the keychain if necessary.
    ///
    /// - Parameters:
    ///   - copyResult: The result from the `SecItemCopyMatching` done by `persistentReferenceFor(service:account:password:)`.
    ///   - service: The service name for the item.
    ///   - account: The account for the item.
    ///   - password: The desired password.
    /// - Returns: A persistent reference to the item.
    /// - Throws: Any error returned by the Security framework.

    private static func persistentReferenceByUpdating(copyResult: CFTypeRef, service: String, account: String, password: Data) throws -> Data {
        let copyResult = copyResult as! [String:Any]
        let persistentRef = copyResult[kSecValuePersistentRef as String] as! NSData as Data
        let currentPassword = copyResult[kSecValueData as String] as! NSData as Data
        if password != currentPassword {
            let err = SecItemUpdate([
                kSecClass: kSecClassGenericPassword, 
                kSecAttrService: service, 
                kSecAttrAccount: account, 
            ] as NSDictionary, [
                kSecValueData: password
            ] as NSDictionary)
            try throwOSStatus(err)
        }
        return persistentRef
    }

    /// Returns a persistent reference for a generic password keychain item by adding it to 
    /// the keychain.
    ///
    /// - Parameters:
    ///   - service: The service name for the item.
    ///   - account: The account for the item.
    ///   - password: The desired password.
    /// - Returns: A persistent reference to the item.
    /// - Throws: Any error returned by the Security framework.

    private static func persistentReferenceByAdding(service: String, account: String, password: Data) throws -> Data {
        var addResult: CFTypeRef? = nil
        let err = SecItemAdd([
            kSecClass: kSecClassGenericPassword, 
            kSecAttrService: service, 
            kSecAttrAccount: account, 
            kSecValueData: password,  
            kSecReturnPersistentRef: true, 
        ] as NSDictionary, &addResult)
        try throwOSStatus(err)
        return addResult! as! NSData as Data
    }

    /// Throws an error if a Security framework call has failed.
    ///
    /// - Parameter err: The error to check.

    private static func throwOSStatus(_ err: OSStatus) throws {
        guard err == errSecSuccess else {
            throw NSError(domain: NSOSStatusErrorDomain, code: Int(err), userInfo: nil)
        }
    }
}