-
Notifications
You must be signed in to change notification settings - Fork 2
/
ITSVPNManager.m
195 lines (153 loc) · 6.63 KB
/
ITSVPNManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// ITSVPNClass.m
// Simple VPN Wrapper
//
// Created by Benjamin de Bos on 21-06-15.
// Copyright (c) 2015 ITS-Vision. All rights reserved.
//
#import "ITSVPNManager.h"
#define kVpnKeychain @"com.vpnservice.keychain"
@implementation ITSVPNManager
@synthesize vpnmanager = _vpnmanager;
-(id) init {
if ([super init] == self) {
_vpnmanager = [NEVPNManager sharedManager];
}
return self;
}
-(void)checkIfProfileIsInstalled:(void (^)(BOOL isInstalled))completionBlock {
[_vpnmanager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
if (error) {
NSLog(@"Error occured: %@",error.localizedDescription);
}
if ([[NSString stringWithFormat:@"%@",_vpnmanager.protocol] rangeOfString:@"persistentReference"].location != NSNotFound) {
completionBlock(YES);
} else {
completionBlock(NO);
}
}];
}
-(void)removeVpnConnection:(void (^)(BOOL isRemoved))completionBlock {
[_vpnmanager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
[_vpnmanager removeFromPreferencesWithCompletionHandler:^(NSError *error) {
if (error)
completionBlock(NO);
else
completionBlock(YES);
}];
}];
}
-(void)createProfileForServer:(NSString*)server
withUsername:(NSString*)username
andPassword:(NSData*)password
andSecret:(NSData*)secret
withTitle:(NSString*)title
andProtocol:(NSString*)protocol
completionBlock:(void (^)(void))completionBlock {
[_vpnmanager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
if ([protocol isEqualToString:@"ipsec"]) {
NEVPNProtocolIPSec *ipsec = [[NEVPNProtocolIPSec alloc] init];
ipsec.username = username;
ipsec.serverAddress = server;
ipsec.passwordReference = password;
ipsec.authenticationMethod = NEVPNIKEAuthenticationMethodSharedSecret;
ipsec.sharedSecretReference = secret;
ipsec.disconnectOnSleep = NO;
ipsec.useExtendedAuthentication = YES;
[_vpnmanager setProtocol:ipsec];
[_vpnmanager setOnDemandEnabled:YES];
[_vpnmanager setLocalizedDescription:[NSString stringWithFormat:@"UmbraVPN IPSec - %@",server]];
[_vpnmanager setEnabled:YES];
} else {
NEVPNProtocolIKEv2 *p = [[NEVPNProtocolIKEv2 alloc] init];
p.username = username;
p.passwordReference = password;
p.serverAddress = server;
p.authenticationMethod = NEVPNIKEv2EncryptionAlgorithmDES;
p.sharedSecretReference = secret;
p.disconnectOnSleep = NO;
p.useExtendedAuthentication = YES;
[_vpnmanager setProtocol:p];
[_vpnmanager setOnDemandEnabled:YES];
[_vpnmanager setLocalizedDescription:[NSString stringWithFormat:@"UmbraVPN IKEv2 - %@",server]];
[_vpnmanager setEnabled:YES];
}
[_vpnmanager saveToPreferencesWithCompletionHandler:^(NSError *error) {
if(error) {
NSLog(@"Save error: %@", error);
} else {
completionBlock();
}
}];
}];
}
-(void)startVpnTunnel {
[_vpnmanager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
NSError *startError;
[_vpnmanager.connection startVPNTunnelAndReturnError:&startError];
if(startError) {
NSLog(@"Start error: %@", startError.localizedDescription);
}
}];
}
-(void)stopVPNConnection {
[_vpnmanager loadFromPreferencesWithCompletionHandler:^(NSError *error) {
[_vpnmanager.connection stopVPNTunnel];
}];
}
-(NSMutableDictionary *)buildDefaultDictionaryForIdentity:(NSString*)identifier {
NSMutableDictionary *searchDictionary = [[NSMutableDictionary alloc] init];
NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
searchDictionary[(__bridge id)kSecClass] = (__bridge id)kSecClassGenericPassword;
searchDictionary[(__bridge id)kSecAttrGeneric] = encodedIdentifier;
searchDictionary[(__bridge id)kSecAttrAccount] = encodedIdentifier;
searchDictionary[(__bridge id)kSecAttrService] = kVpnKeychain;
return searchDictionary;
}
- (NSData *)getKeyChainItemReferenceFromIdentifier:(NSString *)identifier {
return [self getKeyChainItemReferenceFromIdentifier:identifier returnReference:YES];
}
- (NSData *)getKeyChainItemReferenceFromIdentifier:(NSString *)identifier returnReference:(BOOL)referenceOnly{
//get default dictionary
NSMutableDictionary *dict = [self buildDefaultDictionaryForIdentity:identifier];
//set for searching
dict[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;
//need reference
if (referenceOnly)
dict[(__bridge id)kSecReturnPersistentRef] = @YES;
else
dict[(__bridge id)kSecReturnData] = @YES;
//create result object
CFTypeRef result = NULL;
//Get that shit
SecItemCopyMatching((__bridge CFDictionaryRef)dict, &result);
//return that shit
return (__bridge_transfer NSData *)result;
}
//got from: http://useyourloaf.com/blog/2010/03/29/simple-iphone-keychain-access.html
-(NSString*)getKeyChainStringFromIdentifier:(NSString*)identifier {
NSData *keychainData = [self getKeyChainItemReferenceFromIdentifier:identifier returnReference:NO];
return [[NSString alloc] initWithData:keychainData encoding:NSUTF8StringEncoding];
}
-(BOOL)setKeyChainString:(NSString*)string forIdentifier:(NSString*)identifier {
NSMutableDictionary *searchDictionary = [self buildDefaultDictionaryForIdentity:identifier];
NSData *keychainValue = [string dataUsingEncoding:NSUTF8StringEncoding];
if ([self getKeyChainItemReferenceFromIdentifier:identifier] == nil) {
[searchDictionary setObject:keychainValue forKey:(__bridge id)kSecValueData];
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)searchDictionary, NULL);
if (status == errSecSuccess) {
return YES;
}
return NO;
} else {
NSMutableDictionary *updateDictionary = [[NSMutableDictionary alloc] init];
[updateDictionary setObject:keychainValue forKey:(__bridge id)kSecValueData];
OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)searchDictionary,
(__bridge CFDictionaryRef)updateDictionary);
if (status == errSecSuccess) {
return YES;
}
return NO;
}
}
@end