Guest User

Untitled

a guest
May 18th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4.  
  5. // init VPN manager
  6. self.vpnManager = [NEVPNManager sharedManager];
  7.  
  8. // load config from perference
  9. [_vpnManager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
  10.  
  11. if (error) {
  12. NSLog(@"Load config failed [%@]", error.localizedDescription);
  13. return;
  14. }
  15.  
  16. NEVPNProtocolIPSec *p = _vpnManager.protocol;
  17.  
  18. if (p) {
  19. // Protocol exists.
  20. // If you don't want to edit it, just return here.
  21. } else {
  22. // create a new one.
  23. p = [[NEVPNProtocolIPSec alloc] init];
  24. }
  25.  
  26. // config IPSec protocol
  27. p.username = @"[Your username]";
  28. p.serverAddress = @"[Your server address]";;
  29.  
  30. // Get password persistent reference from keychain
  31. // If password doesn't exist in keychain, should create it beforehand.
  32. // [self createKeychainValue:@"your_password" forIdentifier:@"VPN_PASSWORD"];
  33. p.passwordReference = [self searchKeychainCopyMatching:@"VPN_PASSWORD"];
  34.  
  35. // PSK
  36. p.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
  37. // [self createKeychainValue:@"your_psk" forIdentifier:@"PSK"];
  38. p.sharedSecretReference = [self searchKeychainCopyMatching:@"PSK"];
  39.  
  40. /*
  41. // certificate
  42. p.identityData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"client" ofType:@"p12"]];
  43. p.identityDataPassword = @"[Your certificate import password]";
  44. */
  45.  
  46. p.localIdentifier = @"[VPN local identifier]";
  47. p.remoteIdentifier = @"[VPN remote identifier]";
  48.  
  49. p.useExtendedAuthentication = YES;
  50. p.disconnectOnSleep = NO;
  51.  
  52. _vpnManager.protocol = p;
  53. _vpnManager.localizedDescription = @"IPSec Demo";
  54.  
  55. [_vpnManager saveToPreferencesWithCompletionHandler:^(NSError *error) {
  56. if (error) {
  57. NSLog(@"Save config failed [%@]", error.localizedDescription);
  58. }
  59. }];
  60. }];
  61. }
  62.  
  63. - (IBAction)startVPNConnection:(id)sender {
  64. //[[VodManager sharedManager] installVPNProfile];
  65.  
  66. NSError *startError;
  67. [_vpnManager.connection startVPNTunnelAndReturnError:&startError];
  68. if (startError) {
  69. NSLog(@"Start VPN failed: [%@]", startError.localizedDescription);
  70. }
  71. }
  72.  
  73.  
  74. #pragma mark - KeyChain
  75.  
  76. static NSString * const serviceName = @"im.zorro.ipsec_demo.vpn_config";
  77.  
  78. - (NSMutableDictionary *)newSearchDictionary:(NSString *)identifier {
  79. NSMutableDictionary *searchDictionary = [[NSMutableDictionary alloc] init];
  80.  
  81. [searchDictionary setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
  82.  
  83. NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
  84. [searchDictionary setObject:encodedIdentifier forKey:(__bridge id)kSecAttrGeneric];
  85. [searchDictionary setObject:encodedIdentifier forKey:(__bridge id)kSecAttrAccount];
  86. [searchDictionary setObject:serviceName forKey:(__bridge id)kSecAttrService];
  87.  
  88. return searchDictionary;
  89. }
  90.  
  91. - (NSData *)searchKeychainCopyMatching:(NSString *)identifier {
  92. NSMutableDictionary *searchDictionary = [self newSearchDictionary:identifier];
  93.  
  94. // Add search attributes
  95. [searchDictionary setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
  96.  
  97. // Add search return types
  98. // Must be persistent ref !!!!
  99. [searchDictionary setObject:@YES forKey:(__bridge id)kSecReturnPersistentRef];
  100.  
  101. CFTypeRef result = NULL;
  102. SecItemCopyMatching((__bridge CFDictionaryRef)searchDictionary, &result);
  103.  
  104. return (__bridge_transfer NSData *)result;
  105. }
  106.  
  107. - (BOOL)createKeychainValue:(NSString *)password forIdentifier:(NSString *)identifier {
  108. NSMutableDictionary *dictionary = [self newSearchDictionary:identifier];
  109.  
  110. OSStatus status = SecItemDelete((__bridge CFDictionaryRef)dictionary);
  111.  
  112. NSData *passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
  113. [dictionary setObject:passwordData forKey:(__bridge id)kSecValueData];
  114.  
  115. status = SecItemAdd((__bridge CFDictionaryRef)dictionary, NULL);
  116.  
  117. if (status == errSecSuccess) {
  118. return YES;
  119. }
  120. return NO;
  121. }
Add Comment
Please, Sign In to add comment