-
Notifications
You must be signed in to change notification settings - Fork 0
/
VOKeyChainManagement.m
150 lines (115 loc) · 5.09 KB
/
VOKeyChainManagement.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
//
// VOKeyChainManagement.m
// voPlayer
//
// Created by voApp on 5/8/14.
// Copyright (c) 2014 Lin Cheng. All rights reserved.
//
#import "VOKeyChainManagement.h"
#import <Security/Security.h>
static NSString *keychainAttributeServiceName = @"com.aes128crypt.keychainAttributeServiceName";
@implementation VOKeyChainManagement
+ (NSMutableDictionary *)newSearchDictionary
{
NSMutableDictionary *searchDictionary = [[NSMutableDictionary alloc] init];
[searchDictionary setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
[searchDictionary setObject:keychainAttributeServiceName forKey:(id)kSecAttrService];
return searchDictionary;
}
+ (BOOL)saveKeyData:(NSString *)data withKeyID:(NSString *)identifier
{
NSMutableDictionary *searchDictionary = [self newSearchDictionary];
NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrGeneric];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrAccount];
NSData *passwordData = [data dataUsingEncoding:NSUTF8StringEncoding];
[searchDictionary setObject:passwordData forKey:(id)kSecValueData];
NSData *result = nil;
OSStatus status = SecItemAdd((CFDictionaryRef)searchDictionary, (CFTypeRef *)&result);
[searchDictionary release];
if (status == errSecSuccess) {
return YES;
} else if (status == errSecDuplicateItem) {
return [self updateKeychainValue:data forIdentifier:identifier];
}
return NO;
}
+ (NSData *)retrieveKeyData:(NSString *)identifier
{
NSMutableDictionary *searchDictionary = [self newSearchDictionary];
NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrGeneric];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrAccount];
// Add search attributes
[searchDictionary setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
// Add search return types
[searchDictionary setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
NSData *result = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)searchDictionary,
(CFTypeRef *)&result);
if (status != errSecSuccess) {
//NSLog(@"status = %d", (int)status);
}
[searchDictionary release];
return result;
}
+ (BOOL)updateKeyData:(NSString *)data withKeyID:(NSString *)identifier
{
NSMutableDictionary *searchDictionary = [self newSearchDictionary];
NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrGeneric];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrAccount];
NSMutableDictionary *updateDictionary = [[NSMutableDictionary alloc] init];
NSData *passwordData = [data dataUsingEncoding:NSUTF8StringEncoding];
[updateDictionary setObject:passwordData forKey:(id)kSecValueData];
OSStatus status = SecItemUpdate((CFDictionaryRef)searchDictionary,
(CFDictionaryRef)updateDictionary);
[searchDictionary release];
[updateDictionary release];
if (status == errSecSuccess) {
return YES;
}
return NO;
}
+ (BOOL)updateKeychainValue:(NSString *)data forIdentifier:(NSString *)identifier
{
NSMutableDictionary *searchDictionary = [self newSearchDictionary];
NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrGeneric];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrAccount];
NSMutableDictionary *updateDictionary = [[NSMutableDictionary alloc] init];
NSData *passwordData = [data dataUsingEncoding:NSUTF8StringEncoding];
[updateDictionary setObject:passwordData forKey:(id)kSecValueData];
OSStatus status = SecItemUpdate((CFDictionaryRef)searchDictionary,
(CFDictionaryRef)updateDictionary);
[searchDictionary release];
[updateDictionary release];
if (status == errSecSuccess) {
return YES;
}
return NO;
}
+ (BOOL)deleteKeyData:(NSString *)identifier
{
NSMutableDictionary *searchDictionary = [self newSearchDictionary];
NSData *encodedIdentifier = [identifier dataUsingEncoding:NSUTF8StringEncoding];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrGeneric];
[searchDictionary setObject:encodedIdentifier forKey:(id)kSecAttrAccount];
OSStatus status = SecItemDelete((CFDictionaryRef)searchDictionary);
[searchDictionary release];
if (status == errSecSuccess) {
return YES;
}
return NO;
}
+ (BOOL)deleteAllKeyDataUseInApp
{
NSMutableDictionary *searchDictionary = [self newSearchDictionary];
OSStatus status = SecItemDelete((CFDictionaryRef)searchDictionary);
[searchDictionary release];
if (status == errSecSuccess) {
return YES;
}
return NO;
}
@end