forked from mus1cjunk1e/NativaPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EMKeychainItem.m
508 lines (414 loc) · 13.7 KB
/
EMKeychainItem.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/*Copyright (c) 2009 Extendmac, LLC. <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#import "EMKeychainItem.h"
@interface EMKeychainItem (Private)
/*!
@abstract Modifies the given attribute to be newValue.
@param attributeTag The attribute's tag.
@param newValue A pointer to the new value.
@param newLength The length of the new value.
*/
- (void)_modifyAttributeWithTag:(SecItemAttr)attributeTag toBeValue:(void *)newValue ofLength:(UInt32)newLength;
@end
@implementation EMKeychainItem
static BOOL _logsErrors;
+ (void)lockKeychain
{
SecKeychainLock(NULL);
}
+ (void)unlockKeychain
{
SecKeychainUnlock(NULL, 0, NULL, NO);
}
+ (BOOL)logsErrors
{
@synchronized (self)
{
return _logsErrors;
}
return NO;
}
+ (void)setLogsErrors:(BOOL)logsErrors
{
@synchronized (self)
{
if (_logsErrors == logsErrors)
return;
_logsErrors = logsErrors;
}
}
#pragma mark -
- (id)_initWithCoreKeychainItem:(SecKeychainItemRef)item
username:(NSString *)username
password:(NSString *)password
{
if ((self = [super init]))
{
mCoreKeychainItem = item;
mUsername = [username copy];
mPassword = [password copy];
return self;
}
return nil;
}
- (void)_modifyAttributeWithTag:(SecItemAttr)attributeTag toBeValue:(void *)newValue ofLength:(UInt32)newLength
{
NSAssert(mCoreKeychainItem, @"Core keychain item is nil. You cannot modify a keychain item that is not in the keychain.");
SecKeychainAttribute attributes[1];
attributes[0].tag = attributeTag;
attributes[0].length = newLength;
attributes[0].data = newValue;
SecKeychainAttributeList attributeList;
attributeList.count = 1;
attributeList.attr = attributes;
SecKeychainItemModifyAttributesAndData(mCoreKeychainItem, &attributeList, 0, NULL);
}
- (void)dealloc
{
[mUsername release];
[mPassword release];
[mLabel release];
if (mCoreKeychainItem)
CFRelease(mCoreKeychainItem);
[super dealloc];
}
#pragma mark General Properties
@dynamic password;
- (void)setPassword:(NSString *)newPassword
{
@synchronized (self)
{
if (mPassword == newPassword)
return;
[mPassword release];
mPassword = [newPassword copy];
const char *newPasswordCString = [newPassword UTF8String];
SecKeychainItemModifyAttributesAndData(mCoreKeychainItem, NULL, strlen(newPasswordCString), (void *)newPasswordCString);
}
}
- (NSString *) password
{
return mPassword;
}
@dynamic username;
- (void)setUsername:(NSString *)newUsername
{
@synchronized (self)
{
if (mUsername == newUsername)
return;
[mUsername release];
mUsername = [newUsername copy];
const char *newUsernameCString = [newUsername UTF8String];
[self _modifyAttributeWithTag:kSecAccountItemAttr toBeValue:(void *)newUsernameCString ofLength:strlen(newUsernameCString)];
}
}
-(NSString*) username
{
return mUsername;
}
@dynamic label;
- (void)setLabel:(NSString *)newLabel
{
@synchronized (self)
{
if (mLabel == newLabel)
return;
[mLabel release];
mLabel = [newLabel copy];
const char *newLabelCString = [newLabel UTF8String];
[self _modifyAttributeWithTag:kSecLabelItemAttr toBeValue:(void *)newLabelCString ofLength:strlen(newLabelCString)];
}
}
-(NSString*) label
{
return mLabel;
}
#pragma mark Actions
- (void)removeFromKeychain
{
NSAssert(mCoreKeychainItem, @"Core keychain item is nil. You cannot remove a keychain item that is not in the keychain already.");
if (mCoreKeychainItem)
{
OSStatus resultStatus = SecKeychainItemDelete(mCoreKeychainItem);
if (resultStatus == noErr)
{
CFRelease(mCoreKeychainItem);
mCoreKeychainItem = nil;
}
}
}
@end
#pragma mark -
@implementation EMGenericKeychainItem
- (id)_initWithCoreKeychainItem:(SecKeychainItemRef)item
serviceName:(NSString *)serviceName
username:(NSString *)username
password:(NSString *)password
{
if ((self = [super _initWithCoreKeychainItem:item username:username password:password]))
{
mServiceName = [serviceName copy];
return self;
}
return nil;
}
+ (id)_genericKeychainItemWithCoreKeychainItem:(SecKeychainItemRef)coreKeychainItem
forServiceName:(NSString *)serviceName
username:(NSString *)username
password:(NSString *)password
{
return [[[EMGenericKeychainItem alloc] _initWithCoreKeychainItem:coreKeychainItem
serviceName:serviceName
username:username
password:password] autorelease];
}
- (void)dealloc
{
[mServiceName release];
[super dealloc];
}
#pragma mark -
+ (EMGenericKeychainItem *)genericKeychainItemForService:(NSString *)serviceName
withUsername:(NSString *)username
{
if (!serviceName || !username)
return nil;
const char *serviceNameCString = [serviceName UTF8String];
const char *usernameCString = [username UTF8String];
UInt32 passwordLength = 0;
char *password = nil;
SecKeychainItemRef item = nil;
OSStatus returnStatus = SecKeychainFindGenericPassword(NULL, strlen(serviceNameCString), serviceNameCString, strlen(usernameCString), usernameCString, &passwordLength, (void **)&password, &item);
if (returnStatus != noErr || !item)
{
if (_logsErrors)
NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus));
return nil;
}
NSString *passwordString = [[[NSString alloc] initWithData:[NSData dataWithBytes:password length:passwordLength] encoding:NSUTF8StringEncoding] autorelease];
SecKeychainItemFreeContent(NULL, password);
return [EMGenericKeychainItem _genericKeychainItemWithCoreKeychainItem:item forServiceName:serviceName username:username password:passwordString];
}
+ (EMGenericKeychainItem *)addGenericKeychainItemForService:(NSString *)serviceName
withUsername:(NSString *)username
password:(NSString *)password
{
if (!serviceName || !username || !password)
return nil;
const char *serviceNameCString = [serviceName UTF8String];
const char *usernameCString = [username UTF8String];
const char *passwordCString = [password UTF8String];
SecKeychainItemRef item = nil;
OSStatus returnStatus = SecKeychainAddGenericPassword(NULL, strlen(serviceNameCString), serviceNameCString, strlen(usernameCString), usernameCString, strlen(passwordCString), (void *)passwordCString, &item);
if (returnStatus != noErr || !item)
{
if (_logsErrors)
NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus));
return nil;
}
return [EMGenericKeychainItem _genericKeychainItemWithCoreKeychainItem:item forServiceName:serviceName username:username password:password];
}
#pragma mark Generic Properties
@dynamic serviceName;
- (void)setServiceName:(NSString *)newServiceName
{
@synchronized (self)
{
if (mServiceName == newServiceName)
return;
[mServiceName release];
mServiceName = [newServiceName copy];
const char *newServiceNameCString = [newServiceName UTF8String];
[self _modifyAttributeWithTag:kSecServiceItemAttr toBeValue:(void *)newServiceNameCString ofLength:strlen(newServiceNameCString)];
}
}
- (NSString*) serviceName
{
return mServiceName;
}
@end
#pragma mark -
@implementation EMInternetKeychainItem
- (id)_initWithCoreKeychainItem:(SecKeychainItemRef)item
server:(NSString *)server
username:(NSString *)username
password:(NSString *)password
path:(NSString *)path
port:(NSInteger)port
protocol:(SecProtocolType)protocol
{
if ((self = [super _initWithCoreKeychainItem:item username:username password:password]))
{
mServer = [server copy];
mPath = [path copy];
mPort = port;
mProtocol = protocol;
return self;
}
return nil;
}
- (void)dealloc
{
[mServer release];
[mPath release];
[super dealloc];
}
+ (id)_internetKeychainItemWithCoreKeychainItem:(SecKeychainItemRef)coreKeychainItem
forServer:(NSString *)server
username:(NSString *)username
password:(NSString *)password
path:(NSString *)path
port:(NSInteger)port
protocol:(SecProtocolType)protocol
{
return [[[EMInternetKeychainItem alloc] _initWithCoreKeychainItem:coreKeychainItem
server:server
username:username
password:password
path:path
port:port
protocol:protocol] autorelease];
}
#pragma mark -
+ (EMInternetKeychainItem *)internetKeychainItemForServer:(NSString *)server
withUsername:(NSString *)username
path:(NSString *)path
port:(NSInteger)port
protocol:(SecProtocolType)protocol
{
if (!server || !username)
return nil;
const char *serverCString = [server UTF8String];
const char *usernameCString = [username UTF8String];
const char *pathCString = [path UTF8String];
if (!path || [path length] == 0)
pathCString = "";
UInt32 passwordLength = 0;
char *password = nil;
SecKeychainItemRef item = nil;
//0 is kSecAuthenticationTypeAny
OSStatus returnStatus = SecKeychainFindInternetPassword(NULL, strlen(serverCString), serverCString, 0, NULL, strlen(usernameCString), usernameCString, strlen(pathCString), pathCString, port, protocol, 0, &passwordLength, (void **)&password, &item);
if (returnStatus != noErr && protocol == kSecProtocolTypeFTP)
{
//Some clients (like Transmit) still save passwords with kSecProtocolTypeFTPAccount, which was deprecated. Let's check for that.
protocol = kSecProtocolTypeFTPAccount;
returnStatus = SecKeychainFindInternetPassword(NULL, strlen(serverCString), serverCString, 0, NULL, strlen(usernameCString), usernameCString, strlen(pathCString), pathCString, port, protocol, 0, &passwordLength, (void **)&password, &item);
}
if (returnStatus != noErr || !item)
{
if (_logsErrors)
NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus));
return nil;
}
NSString *passwordString = [[[NSString alloc] initWithData:[NSData dataWithBytes:password length:passwordLength] encoding:NSUTF8StringEncoding] autorelease];
SecKeychainItemFreeContent(NULL, password);
return [EMInternetKeychainItem _internetKeychainItemWithCoreKeychainItem:item forServer:server username:username password:passwordString path:path port:port protocol:protocol];
}
+ (EMInternetKeychainItem *)addInternetKeychainItemForServer:(NSString *)server
withUsername:(NSString *)username
password:(NSString *)password
path:(NSString *)path
port:(NSInteger)port
protocol:(SecProtocolType)protocol
{
if (!username || !server || !password)
return nil;
const char *serverCString = [server UTF8String];
const char *usernameCString = [username UTF8String];
const char *passwordCString = [password UTF8String];
const char *pathCString = [path UTF8String];
if (!path || [path length] == 0)
pathCString = "";
SecKeychainItemRef item = nil;
OSStatus returnStatus = SecKeychainAddInternetPassword(NULL, strlen(serverCString), serverCString, 0, NULL, strlen(usernameCString), usernameCString, strlen(pathCString), pathCString, port, protocol, kSecAuthenticationTypeDefault, strlen(passwordCString), (void *)passwordCString, &item);
if (returnStatus != noErr || !item)
{
if (_logsErrors)
NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus));
return nil;
}
return [EMInternetKeychainItem _internetKeychainItemWithCoreKeychainItem:item forServer:server username:username password:password path:path port:port protocol:protocol];
}
#pragma mark Internet Properties
@dynamic server;
- (void)setServer:(NSString *)newServer
{
@synchronized (self)
{
if (mServer == newServer)
return;
[mServer release];
mServer = [newServer copy];
const char *newServerCString = [newServer UTF8String];
[self _modifyAttributeWithTag:kSecServerItemAttr toBeValue:(void *)newServerCString ofLength:strlen(newServerCString)];
}
}
- (NSString *) server
{
return mServer;
}
@dynamic path;
- (void)setPath:(NSString *)newPath
{
if (mPath == newPath)
return;
[mPath release];
mPath = [newPath copy];
const char *newPathCString = [newPath UTF8String];
[self _modifyAttributeWithTag:kSecPathItemAttr toBeValue:(void *)newPathCString ofLength:strlen(newPathCString)];
}
- (NSString *) path
{
return mPath;
}
@dynamic port;
- (void)setPort:(NSInteger)newPort
{
@synchronized (self)
{
if (mPort == newPort)
return;
mPort = newPort;
UInt32 newPortValue = newPort;
[self _modifyAttributeWithTag:kSecPortItemAttr toBeValue:&newPortValue ofLength:sizeof(newPortValue)];
}
}
- (NSInteger) port
{
return mPort;
}
@dynamic protocol;
- (void)setProtocol:(SecProtocolType)newProtocol
{
@synchronized (self)
{
if (mProtocol == newProtocol)
return;
mProtocol = newProtocol;
[self _modifyAttributeWithTag:kSecProtocolItemAttr toBeValue:&newProtocol ofLength:sizeof(newProtocol)];
}
}
-(SecProtocolType) protocol
{
return mProtocol;
}
@end