-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeychain.m
executable file
·140 lines (105 loc) · 3.95 KB
/
Keychain.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
//
// Keychain.m
// LabStatus
//
// Created by Christian Pape on 15.10.08.
// Copyright 2008 cc-productions. All rights reserved.
//
#import "Keychain.h"
@implementation Keychain
- (id)init
{
if (![super init])
return nil;
return self;
}
- (void)removeKeyChainEntries
{
SecKeychainItemRef item;
SecKeychainSearchRef search;
OSStatus status;
OSErr result;
const char *itemKind = [@"LabStatus" UTF8String];
SecKeychainAttribute attrs[] = {
{ kSecDescriptionItemAttr, strlen(itemKind), (char *)itemKind }
};
SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]), attrs };
result = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, &attributes, &search);
while (SecKeychainSearchCopyNext (search, &item) == noErr) {
status = SecKeychainItemDelete(item);
if (item)
CFRelease (item);
if (status != 0) {
NSLog(@"Error deleting keychain entry: %d\n", (int)status);
}
}
if (search)
CFRelease(search);
}
- (void) addKeyChainEntry:(NSString*)hostname username:(NSString*)username password:(NSString*)password
{
OSStatus status;
SecKeychainItemRef item = nil;
const char *itemName = [hostname UTF8String];
const char *itemKind = [@"LabStatus" UTF8String];
const char *accountUsername = [username UTF8String];
const char *accountPassword = [password UTF8String];
SecKeychainAttribute attrs[] = {
{ kSecLabelItemAttr, strlen(itemName), (char *)itemName },
{ kSecDescriptionItemAttr, strlen(itemKind), (char *)itemKind },
{ kSecAccountItemAttr, strlen(accountUsername), (char *)accountUsername },
{ kSecServerItemAttr, strlen(itemName), (char *)itemName }
};
SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]), attrs };
status = SecKeychainItemCreateFromContent(
kSecInternetPasswordItemClass,
&attributes,
strlen(accountPassword),
accountPassword,
NULL, // use the default keychain
NULL,
&item);
if (item)
CFRelease(item);
}
- (NSString*) getKeychainPassword:(NSString*)hostname username:(NSString*)username
{
char *passwordData;
UInt32 passwordLength;
const char *itemName = [hostname UTF8String];
const char *itemKind = [@"LabStatus" UTF8String];
const char *accountUsername = [username UTF8String];
SecKeychainSearchRef search;
SecKeychainItemRef item;
SecKeychainAttribute attrs[] = {
{ kSecLabelItemAttr, strlen(itemName), (char *)itemName },
{ kSecDescriptionItemAttr, strlen(itemKind), (char *)itemKind },
{ kSecAccountItemAttr, strlen(accountUsername), (char *)accountUsername },
};
SecKeychainAttributeList attributes = { sizeof(attrs) / sizeof(attrs[0]), attrs };
OSErr result = SecKeychainSearchCreateFromAttributes(NULL, kSecInternetPasswordItemClass, &attributes, &search);
if (result != noErr) {
NSLog (@"SecKeychainSearchCreateFromAttributes: %d\n", result);
CFRelease(search);
return @"";
}
NSString *returnPassword = @"";
if (SecKeychainSearchCopyNext (search, &item) == noErr) {
SecKeychainAttribute attrs2[] = {
{ kSecAccountItemAttr },
{ kSecDescriptionItemAttr },
{ kSecLabelItemAttr },
{ kSecModDateItemAttr }
};
SecKeychainAttributeList attributes2 = { sizeof(attrs2) / sizeof(attrs2[0]), attrs2 };
OSStatus status = SecKeychainItemCopyContent (item, NULL, &attributes2, &passwordLength, (void**)&passwordData);
if (status == noErr) {
returnPassword = [NSString stringWithCString:passwordData length:passwordLength];
}
SecKeychainItemFreeContent (&attributes2, passwordData);
CFRelease(item);
CFRelease(search);
}
return returnPassword;
}
@end