-
Notifications
You must be signed in to change notification settings - Fork 0
/
KeyChainWrapper.m
202 lines (133 loc) · 5.98 KB
/
KeyChainWrapper.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
//
// KeyChainWrapper.m
//
// Licensed by ruralcoder.com under the
// Creative Commons Attribution-ShareAlike 3.0 Unported License
#import "KeyChainWrapper.h"
#import <Security/Security.h>
@implementation KeyChainWrapper
+ (NSString *) keyChainErrorToString:(OSStatus)status
{
switch (status) {
case errSecSuccess:
return @"errSecSuccess: No error";
case errSecUnimplemented:
return @"errSecUnimplemented: Function or operation not implemented.";
case errSecParam:
return @"errSecParam: One or more parameters passed to the function were not valid.";
case errSecAllocate:
return @"errSecAllocate: Failed to allocate memory.";
case errSecNotAvailable:
return @"errSecNotAvailable: No trust results are available.";
case errSecDuplicateItem:
return @"errSecDuplicateItem: The item already exists.";
case errSecItemNotFound:
return @"errSecItemNotFound: The item cannot be found.";
case errSecInteractionNotAllowed:
return @"errSecInteractionNotAllowed: Interaction with the Security Server is not allowed.";
case errSecDecode:
return @"errSecDecode: Unable to decode the provided data.";
default:
return @"Unknown error from KeyChain services.";
}
}
+ (NSMutableDictionary*) newSearchQueryForUser:(NSString*)userEmail forService:(NSString*)service
{
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:service forKey:(id)kSecAttrService];
[query setObject:userEmail forKey:(id)kSecAttrAccount];
return query;
}
+ (NSMutableDictionary*) newSearchQueryForService:(NSString*)service
{
NSMutableDictionary *query = [[NSMutableDictionary alloc] init];
[query setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[query setObject:service forKey:(id)kSecAttrService];
return query;
}
+ (void) printAttributes:(NSDictionary *)attributes
{
NSArray *keys = [attributes allKeys];
for (NSString *key in keys)
{
// NSLog(@" %@ = %@", key, [attributes objectForKey:key]);
}
}
+ (NSDictionary*) searchForUserEmail:(NSString*)userEmail forService:(NSString*)service
{
// NSLog(@"userEmail: '%@' service: '%@'", userEmail, service);
NSMutableDictionary *searchQuery = [KeyChainWrapper newSearchQueryForUser:userEmail forService:service];
[searchQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnAttributes];
[searchQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
NSMutableDictionary *searchResults = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)searchQuery, (CFTypeRef *)&searchResults);
[searchQuery release];
// NSLog(@" KeyChain status: %@", [KeyChainWrapper keyChainErrorToString:status]);
if (status != errSecSuccess)
return nil;
return [searchResults autorelease];
}
+ (BOOL) createEntryForUser:(NSString*)userEmail entryValue:(NSString*)entryValue forService:(NSString*)service
{
// NSLog(@"userEmail: '%@' service: '%@'", userEmail, service);
NSDictionary *searchResults = [KeyChainWrapper searchForUserEmail:userEmail forService:service];
OSStatus status = errSecSuccess;
if (searchResults != nil)
[KeyChainWrapper removeEntryForUserEmail:userEmail forService:service];
NSData* encodedValue = [entryValue dataUsingEncoding:NSUTF8StringEncoding];
NSMutableDictionary *searchQuery = nil;
searchQuery = [KeyChainWrapper newSearchQueryForUser:userEmail forService:service];
[searchQuery setObject:encodedValue forKey:(id)kSecValueData];
[searchQuery setObject:(id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible];
status = SecItemAdd((CFDictionaryRef)searchQuery, NULL);
if (status != errSecSuccess)
[self printAttributes:searchQuery];
// NSLog(@" KeyChain status: %@", [KeyChainWrapper keyChainErrorToString:status]);
[searchQuery release];
return (status == errSecSuccess);
}
+ (NSString*) retrieveEntryForUser:(NSString*)userEmail forService:(NSString*)service
{
// NSLog(@"userEmail: '%@' service: '%@'", userEmail, service);
NSMutableDictionary *searchQuery = [KeyChainWrapper newSearchQueryForUser:userEmail forService:service];
[searchQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
NSData *entryData = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)searchQuery, (CFTypeRef *)&entryData);
// NSLog(@" KeyChain status: %@", [KeyChainWrapper keyChainErrorToString:status]);
[searchQuery release];
if (status != errSecSuccess)
return nil;
NSString *decodedValue = [[NSString alloc] initWithBytes:[entryData bytes]
length:[entryData length]
encoding:NSUTF8StringEncoding];
[entryData release];
return [decodedValue autorelease];
}
+ (BOOL) isEntryStoredForUserEmail:(NSString*)userEmail forService:(NSString*)service
{
// NSLog(@"userEmail: '%@' service: '%@'", userEmail, service);
NSMutableDictionary *searchQuery = [KeyChainWrapper newSearchQueryForUser:userEmail forService:service];
[searchQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
NSData *entryData = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)searchQuery, (CFTypeRef *)&entryData);
// NSLog(@" KeyChain status: %@", [KeyChainWrapper keyChainErrorToString:status]);
if (status != errSecSuccess)
[self printAttributes:searchQuery];
[searchQuery release];
[entryData release];
return (status == errSecSuccess);
}
+ (BOOL) removeEntryForUserEmail:(NSString*)userEmail forService:(NSString*)service
{
// NSLog(@"userEmail: '%@' service: '%@'", userEmail, service);
NSMutableDictionary *searchQuery = [KeyChainWrapper newSearchQueryForUser:userEmail forService:service];
[searchQuery removeObjectForKey:(id)kSecValueData];
OSStatus status = SecItemDelete((CFDictionaryRef)searchQuery);
// NSLog(@" KeyChain status: %@", [KeyChainWrapper keyChainErrorToString:status]);
if (status != errSecSuccess)
[self printAttributes:searchQuery];
[searchQuery release];
return (status == errSecSuccess);
}
@end