forked from mgorbach/macfusion2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MFSecurity.m
448 lines (394 loc) · 16.5 KB
/
MFSecurity.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// MFSecurity.m
// MacFusion2
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "MFSecurity.h"
#import "MFFSDelegateProtocol.h"
#import "MFConstants.h"
#import "MFServerProtocol.h"
#import "MFLogging.h"
#import "MFServerFSProtocol.h"
#import "MFFilesystemController.h"
#import "MFClientFS.h"
#import "MFClientPlugin.h"
#import "MFNetworkFS.h"
#import "MFCore.h"
#define self @"MFSECURITY"
# pragma mark Keychain interaction
NSString *serviceNameForFS(MFFilesystem* fs) {
return [NSString stringWithFormat: @"Macfusion: %@", fs.name];
}
NSDictionary *getGenericSecretsForFilesystemAndReturnItem(MFFilesystem *fs, SecKeychainItemRef *itemRef) {
UInt32 passwordLength = 0;
void *passwordData;
NSString *serviceName = serviceNameForFS(fs);
OSStatus error = SecKeychainFindGenericPassword(NULL,
[serviceName lengthOfBytesUsingEncoding: NSUTF8StringEncoding],
[serviceName UTF8String],
[fs.uuid lengthOfBytesUsingEncoding: NSUTF8StringEncoding],
[fs.uuid UTF8String],
&passwordLength,
&passwordData,
itemRef);
if (error == noErr) {
// MFLogS(self, @"Found generic keychain entry");
NSData* secretsData = [NSData dataWithBytes:passwordData length:passwordLength];
// MFLogS(self, @"NSData from keycahin %@", secretsData);
NSDictionary* loadedDataDict = [NSPropertyListSerialization propertyListFromData:secretsData mutabilityOption:NSPropertyListMutableContainers format:NULL errorDescription:nil];
if ([loadedDataDict isKindOfClass:[NSDictionary class]]) {
// MFLogS(self, @"Succesfully loaded secrets dictionary from keychain %@", loadedDataDict);
SecKeychainItemFreeContent(NULL, passwordData);
return loadedDataDict;
} else {
// MFLogS(self, @"Failed to parse data in generic entry. data: %@", loadedDataDict);
return nil;
}
} else {
return nil;
}
}
NSDictionary *getNetworkSecretsForFilesystemAndReturnItem(MFFilesystem *fs, SecKeychainItemRef *itemRef) {
UInt32 passwordLength = 0;
void *passwordData;
NSString *userName = [[fs parameters] objectForKey:kNetFSUserParameter];
int port = [[[fs parameters] objectForKey:kNetFSPortParameter] intValue];
NSString *hostName = [[fs parameters] objectForKey:kNetFSHostParameter];
SecProtocolType protocol = [[[fs parameters] objectForKey:kNetFSProtocolParameter] intValue];
NSString *passwordToReturn = nil;
if (userName && hostName && port && protocol) {
OSStatus error = SecKeychainFindInternetPassword(NULL,[hostName lengthOfBytesUsingEncoding: NSUTF8StringEncoding],[hostName UTF8String],0,NULL,[userName lengthOfBytesUsingEncoding: NSUTF8StringEncoding],[userName UTF8String],0,NULL,port,protocol,(SecAuthenticationType)NULL,&passwordLength,&passwordData,itemRef);
if (error == noErr) {
// MFLogS(self, @"Successfully found internet password for fs %@", fs);
NSString *password = [NSString stringWithCString:passwordData encoding:NSUTF8StringEncoding];
SecKeychainItemFreeContent(NULL, passwordData);
if (password) {
passwordToReturn = password;
}
} else {
// MFLogS(self, @"Error searching for internet password fs %@ error %d", fs, error);
}
} else {
// MFLogS(self, @"No network info to search for fs %@", fs);
}
return passwordToReturn ? [NSDictionary dictionaryWithObject:passwordToReturn forKey:kNetFSPasswordParameter] : nil;
}
NSDictionary *getGenericSecretsForFilesystem(MFFilesystem* fs) {
return getGenericSecretsForFilesystemAndReturnItem(fs, NULL);
}
NSDictionary *getNetworkSecretsForFilesystem(MFFilesystem* fs) {
return getNetworkSecretsForFilesystemAndReturnItem(fs, NULL);
}
NSDictionary *mfsecGetSecretsDictionaryForFilesystem(MFFilesystem* fs) {
NSDictionary *genericSecretsDict = getGenericSecretsForFilesystem(fs);
NSDictionary *networkSecretsDict = getNetworkSecretsForFilesystem(fs);
NSMutableDictionary* secretsDictionary = [NSMutableDictionary dictionary];
NSArray *secretsList = [[fs delegate] secretsList];
for (NSString *secretKey in secretsList) {
id genericSecret = [genericSecretsDict objectForKey: secretKey];
id networkSecret = [networkSecretsDict objectForKey: secretKey];
if (genericSecret) {
[secretsDictionary setObject: genericSecret forKey: secretKey ];
} else if (networkSecret) {
[secretsDictionary setObject: networkSecret forKey: secretKey ];
}
}
if ([secretsDictionary count] > 0) {
return [secretsDictionary copy];
} else {
// MFLogS(self, @"Nothing useful in secrets dictionary. Returning nil");
return nil;
}
}
SecAccessRef keychainAccessRefForFilesystem(MFFilesystem* fs) {
SecAccessRef accessRef;
OSStatus error;
NSArray *trustedApplicationPaths = mfcSecretClientsForFileystem(fs);
NSMutableArray *trustRefs = [NSMutableArray array];
for(NSString *path in trustedApplicationPaths) {
SecTrustedApplicationRef trustedAppRef;
error = SecTrustedApplicationCreateFromPath([path cStringUsingEncoding: NSUTF8StringEncoding], &trustedAppRef);
if (error != noErr) {
// MFLogSO(self, fs, @"Could not create trusted ref for path %@ fs %@ error %d", path, fs, error);
} else {
[trustRefs addObject: (id)trustedAppRef];
}
}
error = SecAccessCreate((CFStringRef)serviceNameForFS( fs ), (CFArrayRef)[trustRefs copy], &accessRef);
if (error != noErr) {
// MFLogSO(self, fs, @"Failed to create access ref for fs %@ error %d", fs, error);
return NULL;
} else {
return accessRef;
}
}
void setNetworkSecretsForFilesystem (NSDictionary* secretsDictionary, MFFilesystem* fs ) {
NSString *userName = [[fs parameters] objectForKey:kNetFSUserParameter];
int port = [[[fs parameters] objectForKey:kNetFSPortParameter] intValue];
NSString *hostName = [[fs parameters] objectForKey:kNetFSHostParameter];
NSString *password = [secretsDictionary objectForKey:kNetFSPasswordParameter];
SecProtocolType protocol = [[[fs parameters] objectForKey:kNetFSProtocolParameter] intValue];
if (userName && hostName && port) {
SecKeychainItemRef itemRef = NULL;
if (getNetworkSecretsForFilesystemAndReturnItem(fs, &itemRef)) {
if ([secretsDictionary count] == 0) {
// Delete
OSErr result = SecKeychainItemDelete( itemRef );
if (result == noErr) {
// MFLogS(self, @"Network keychain item deleted OK");
} else {
// MFLogSO(self, fs, @"Network keychain item deleted failed: %d", result);
}
return;
}
// Modify
OSStatus error = SecKeychainItemModifyContent(itemRef,NULL,[password lengthOfBytesUsingEncoding: NSUTF8StringEncoding],[password UTF8String]);
if (error == noErr) {
// MFLogS(self, @"Successfully modified network secrets for fs %@", fs );
} else {
// MFLogSO(self, fs, @"Failed to modify network secrets for fs %@. Error %d", fs, error);
}
} else {
// Create
SecKeychainAttribute attrs[] = {
{ kSecLabelItemAttr, [hostName lengthOfBytesUsingEncoding: NSUTF8StringEncoding], (char *)[hostName UTF8String] },
{ kSecAccountItemAttr, [userName lengthOfBytesUsingEncoding: NSUTF8StringEncoding], (char *)[userName UTF8String] },
{ kSecServerItemAttr, [hostName lengthOfBytesUsingEncoding: NSUTF8StringEncoding], (char *)[hostName UTF8String] },
{ kSecPortItemAttr, sizeof(int), (int *)&port },
{ kSecProtocolItemAttr, sizeof(SecProtocolType), (SecProtocolType *)&protocol }
};
SecKeychainAttributeList attributes = {
sizeof(attrs)/sizeof(attrs[0]), attrs
};
SecAccessRef accessRef = keychainAccessRefForFilesystem( fs );
SecItemClass itemClass = kSecInternetPasswordItemClass;
OSStatus error = SecKeychainItemCreateFromContent(itemClass,
&attributes,
[password lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
[password UTF8String],
NULL,
accessRef,
&itemRef);
if (error == noErr) {
// MFLogS(self, @"Successfully stored network secrets for fs %@", fs);
} else {
// MFLogSO(self, fs, @"Failed to store network secerets for fs %@. Error %d", fs, error);
}
}
} else {
// MFLogS(self, @"No network info to write for fs %@", fs);
}
return;
}
void setGenericSecretsForFilesystem(NSDictionary* secretsDictionary, MFFilesystem* fs) {
NSString *serializationErrorString;
if (![fs isPersistent]) {
// MFLogS(self, @"Not setting generic secrets for temporary fs %@", fs);
return;
}
NSData *secretsData = [NSPropertyListSerialization dataFromPropertyList:secretsDictionary format:NSPropertyListBinaryFormat_v1_0
errorDescription:&serializationErrorString];
if (secretsData) {
// MFLogS(self, @"Generic secrets serialized OK");
} else {
// MFLogSO(self, fs, @"Could not serialize generic secrets dictionary for fs %@", fs);
return;
}
SecKeychainItemRef itemRef = NULL;
if (getGenericSecretsForFilesystemAndReturnItem(fs, &itemRef) || itemRef ) {
if ([secretsDictionary count] == 0) {
// Delete
OSErr result = SecKeychainItemDelete( itemRef );
if (result == noErr) {
// MFLogS(self, @"Generic keychain item deleted OK");
} else {
// MFLogSO(self, fs, @"Generic keychain item deleted failed: %d fs %@", result, fs);
}
return;
}
// Modify
OSErr result = SecKeychainItemModifyContent(itemRef,
NULL,
[secretsData length],
[secretsData bytes]);
if (result == noErr) {
// MFLogSO(self, fs, @"Generic keychain data updated succesfully fs %@", fs);
return;
} else {
// MFLogSO(self, fs, @"Failed to update generatic keychain data for fs %@. Result %d", fs, result);
}
} else {
// Create
NSString *serviceName = serviceNameForFS(fs);
SecItemClass itemClass = kSecGenericPasswordItemClass;
SecAccessRef accessRef = keychainAccessRefForFilesystem(fs);
if (accessRef == NULL) {
// MFLogSO(self, fs, @"Null access ref for fs %@. Returning", fs);
return;
}
SecKeychainAttribute attrs[] = {
{ kSecLabelItemAttr, [serviceName lengthOfBytesUsingEncoding: NSUTF8StringEncoding], (char*)[serviceName UTF8String] },
{ kSecAccountItemAttr, [fs.uuid lengthOfBytesUsingEncoding: NSUTF8StringEncoding], (char*)[fs.uuid UTF8String] },
{ kSecServiceItemAttr, [serviceName lengthOfBytesUsingEncoding: NSUTF8StringEncoding], (char*)[serviceName UTF8String] }
};
SecKeychainAttributeList attributes = {
sizeof(attrs)/sizeof(attrs[0]), attrs
};
OSErr result = SecKeychainItemCreateFromContent(itemClass,
&attributes,
[secretsData length],
[secretsData bytes],
NULL,
accessRef,
&itemRef);
CFRelease(accessRef);
if (result == noErr) {
// MFLogS(self, @"Generic keychain data created succesfully");
return;
} else {
// MFLogSO(self, fs, @"Failed to create generic keychain data for fs %@. Result %d", fs, result);
return;
}
}
}
void mfsecSetSecretsDictionaryForFilesystem(NSDictionary *secretsDictionary, MFFilesystem *fs) {
// MFLogS(self, @"Setting secrets dict %@ for fs %@", secretsDictionary, fs);
if (!secretsDictionary) {
// MFLogSO(self, fs, @"Secrets dictionary nil for fs %@. Nothing to store to keychain", fs);
return;
}
setGenericSecretsForFilesystem(secretsDictionary, fs);
setNetworkSecretsForFilesystem(secretsDictionary, fs);
}
# pragma mark Token authentication
MFClientFS *mfsecGetFilesystemForToken(NSString* token) {
id <MFServerProtocol> server = (id<MFServerProtocol>)[NSConnection rootProxyForConnectionWithRegisteredName:kMFDistributedObjectName host:nil];
if (server) {
id <MFServerFSProtocol> remoteFS = (id <MFServerFSProtocol>)[server filesystemForToken:token];
if (remoteFS) {
MFClientPlugin *clientPlugin = [[MFClientPlugin alloc] initWithRemotePlugin: [remoteFS plugin]];
MFClientFS *fs = [MFClientFS clientFSWithRemoteFS: remoteFS clientPlugin:clientPlugin];
return fs;
} else {
return nil;
}
} else {
// MFLogS(self, @"Can not connect to server to authenticate token %@", token);
return nil;
}
}
NSString *mfsecTokenForFilesystemWithUUID(NSString *uuid) {
id <MFServerProtocol> server = (id<MFServerProtocol>)[NSConnection rootProxyForConnectionWithRegisteredName:kMFDistributedObjectName host:nil];
NSString* token = [server tokenForFilesystemWithUUID:uuid];
// MFLogS(self, @"Token generated for uuid %@: %@", uuid, token);
return token;
}
# pragma mark UI
SInt32 showDialogForPasswordQuery(MFFilesystem* fs, BOOL* savePassword, NSString** password)
{
// Icon URL from fs
const void* keychainKeys[] = {
kCFUserNotificationAlertHeaderKey,
kCFUserNotificationAlertMessageKey,
kCFUserNotificationTextFieldTitlesKey,
kCFUserNotificationCheckBoxTitlesKey,
kCFUserNotificationAlternateButtonTitleKey,
kCFUserNotificationIconURLKey
};
NSString* iconURL = [NSURL fileURLWithPath: fs.iconPath];
NSString* userName = [[fs parameters] objectForKey: kNetFSUserParameter];
NSString* host = [[fs parameters] objectForKey: kNetFSHostParameter];
NSString* dialogText = [NSString stringWithFormat: @"Please enter network password for host %@ user %@",host, userName];
const void* keychainValues[] = {
@"Password Needed",
dialogText,
@"Password",
@"Save Password in Keychain",
@"Cancel",
iconURL
};
SInt32 error;
CFDictionaryRef dialogTemplate = CFDictionaryCreate(kCFAllocatorDefault,
keychainKeys,
keychainValues,
sizeof(keychainKeys)/sizeof(*keychainKeys),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFUserNotificationRef passwordDialog = CFUserNotificationCreate(kCFAllocatorDefault,
0,
kCFUserNotificationPlainAlertLevel | CFUserNotificationSecureTextField(0),
&error, dialogTemplate);
if (error) {
// MFLogSO(self, fs, @"Dialog error received %d fs %@", error, fs);
}
CFOptionFlags responseFlags;
error = CFUserNotificationReceiveResponse(passwordDialog, 0, &responseFlags);
if (error) {
// MFLogSO(self, fs, @"Dialog error received after received fs %@ response %d", fs, error);
}
CFRelease(dialogTemplate);
CFRelease(passwordDialog);
int button = responseFlags & 0x3;
if (button == kCFUserNotificationAlternateResponse) {
// MFLogSO(self, fs, @"Exiting due to cancel on UI fs %@", fs);
return 1;
}
// This is a hack, checking responseFlags with and correctly wasn't working for some reason
*savePassword = (responseFlags == 256);
// MFLogSO(self, fs, @"Save password is %d Flags %d fs %@", savePassword, responseFlags, fs);
CFStringRef passwordRef = CFUserNotificationGetResponseValue(passwordDialog,kCFUserNotificationTextFieldValuesKey,
0);
*password = (NSString *)passwordRef;
CFRelease(passwordRef);
return 0;
}
NSString *mfsecQueryForFSNetworkPassword(MFClientFS* fs) {
NSDictionary* secrets = mfsecGetSecretsDictionaryForFilesystem( fs );
if ([secrets objectForKey:kNetFSPasswordParameter]) {
// MFLogSO(self, fs, @"Should not be querying if we already have a password fs %@", fs);
return nil;
}
NSString *password = nil;
BOOL save;
[fs setPauseTimeout: YES];
SInt32 result = showDialogForPasswordQuery(fs, &save, &password);
[fs setPauseTimeout: NO];
if (result != 0) {
// MFLogSO(self, fs, @"UI query received results %d fs %@", result, fs);
return nil;
}
if (save && [password length] > 0) {
// MFLogSO(self, fs, @"Updating secrets fs %@", fs);
NSMutableDictionary* updatedSecrets = secrets ? [secrets mutableCopy] : [NSMutableDictionary dictionary];
[updatedSecrets setObject: password forKey: kNetFSPasswordParameter ];
mfsecSetSecretsDictionaryForFilesystem([updatedSecrets copy], fs);
} else {
// MFLogSO(self, fs, @"Not updating secrets %@", fs);
}
return password;
}
NSString *mfsecUUIDForKeychainItemRef(SecKeychainItemRef itemRef) {
SecKeychainAttributeInfo attrInfo;
UInt32 tag = kSecAccountItemAttr;
attrInfo.tag = &tag;
attrInfo.format = NULL;
attrInfo.count = 1;
SecKeychainAttributeList *attrList = NULL;
SecKeychainAttribute *attr = NULL;
SecKeychainItemCopyAttributesAndData(itemRef, &attrInfo, NULL, &attrList, NULL, NULL);
// MFLogS(self, @"Loaded %d attrs", attrList->count);
attr = attrList -> attr;
NSString *uuid = [NSString stringWithCString:attr->data encoding:NSUTF8StringEncoding];
SecKeychainItemFreeAttributesAndData(attrList, NULL);
return uuid;
}