Updated the README for the 0.1 release.
5 // Created by Jens Alfke on 3/26/09.
6 // Copyright 2009 Jens Alfke. All rights reserved.
9 #import "MYKeychainItem.h"
10 #import "MYCrypto_Private.h"
11 #import "MYErrorUtils.h"
14 NSString* const MYCSSMErrorDomain = @"CSSMErrorDomain";
17 @implementation MYKeychainItem
20 - (id) initWithKeychainItemRef: (MYKeychainItemRef)itemRef;
22 Assert(itemRef!=NULL);
32 @synthesize keychainItemRef=_itemRef;
36 if (_itemRef) CFRelease(_itemRef);
42 if (_itemRef) CFRelease(_itemRef);
46 - (id) copyWithZone: (NSZone*)zone {
47 // As keys are immutable, it's not necessary to make copies of them. This makes it more efficient
48 // to use instances as NSDictionary keys or store them in NSSets.
52 - (BOOL) isEqual: (id)obj {
53 return (obj == self) ||
54 ([obj isKindOfClass: [MYKeychainItem class]] && CFEqual(_itemRef, [obj keychainItemRef]));
58 return CFHash(_itemRef);
61 - (NSString*) description {
62 return $sprintf(@"%@[%p]", [self class], _itemRef); //FIX: Can we do anything better?
66 - (NSArray*) _itemList {
67 return $array((id)_itemRef);
70 #if MYCRYPTO_USE_IPHONE_API
71 - (CFDictionaryRef) asQuery {
72 return (CFDictionaryRef) $dict( {(id)kSecClass, (id)kSecClassKey},//FIX
73 {(id)kSecMatchItemList, self._itemList} );
78 - (MYKeychain*) keychain {
79 #if MYCRYPTO_USE_IPHONE_API
80 return [MYKeychain defaultKeychain];
82 MYKeychain *keychain = nil;
83 SecKeychainRef keychainRef = NULL;
84 if (check(SecKeychainItemCopyKeychain((SecKeychainItemRef)_itemRef, &keychainRef), @"SecKeychainItemCopyKeychain")) {
86 keychain = [[[MYKeychain alloc] initWithKeychainRef: keychainRef] autorelease];
87 CFRelease(keychainRef);
94 - (BOOL) removeFromKeychain {
95 #if MYCRYPTO_USE_IPHONE_API
96 return check(SecItemDelete(self.asQuery), @"SecItemDelete");
98 return check(SecKeychainItemDelete((SecKeychainItemRef)_itemRef), @"SecKeychainItemDelete");
104 #pragma mark DATA / METADATA ACCESSORS:
107 - (NSData*) _getContents: (OSStatus*)outError {
108 NSData *contents = nil;
109 #if MYCRYPTO_USE_IPHONE_API
113 *outError = SecKeychainItemCopyAttributesAndData(_itemRef, NULL, NULL, NULL, &length, &bytes);
114 if (!*outError && bytes) {
115 contents = [NSData dataWithBytes: bytes length: length];
116 SecKeychainItemFreeAttributesAndData(NULL, bytes);
122 + (NSData*) _getAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item {
124 #if MYCRYPTO_USE_IPHONE_API
125 NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
126 {(id)kSecMatchItemList, $array((id)item)},
127 {(id)kSecReturnAttributes, $true} );
128 CFDictionaryRef attrs;
129 if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&attrs), @"SecItemCopyMatching"))
131 CFTypeRef rawValue = CFDictionaryGetValue(attrs,attr);
132 value = rawValue ?[[(id)CFMakeCollectable(rawValue) retain] autorelease] :nil;
136 UInt32 format = kSecFormatUnknown;
137 SecKeychainAttributeInfo info = {.count=1, .tag=(UInt32*)&attr, .format=&format};
138 SecKeychainAttributeList *list = NULL;
140 if (check(SecKeychainItemCopyAttributesAndData((SecKeychainItemRef)item, &info,
141 NULL, &list, NULL, NULL),
142 @"SecKeychainItemCopyAttributesAndData")) {
144 if (list->count == 1)
145 value = [NSData dataWithBytes: list->attr->data
146 length: list->attr->length];
147 else if (list->count > 1)
148 Warn(@"Multiple values for keychain item attribute");
149 SecKeychainItemFreeAttributesAndData(list, NULL);
156 + (NSString*) _getStringAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item {
157 NSData *value = [self _getAttribute: attr ofItem: item];
158 if (!value) return nil;
159 const char *bytes = value.bytes;
160 size_t length = value.length;
161 if (length>0 && bytes[length-1] == 0)
162 length--; // Some values are null-terminated!?
163 NSString *str = [[NSString alloc] initWithBytes: bytes length: length
164 encoding: NSUTF8StringEncoding];
166 Warn(@"MYKeychainItem: Couldn't decode attr value as string");
167 return [str autorelease];
170 - (NSString*) stringValueOfAttribute: (SecKeychainAttrType)attr {
171 return [[self class] _getStringAttribute: attr ofItem: _itemRef];
175 + (BOOL) _setAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item
176 stringValue: (NSString*)stringValue
178 #if MYCRYPTO_USE_IPHONE_API
179 id value = stringValue ?(id)stringValue :(id)[NSNull null];
180 NSDictionary *query = $dict({(id)kSecClass, (id)kSecClassKey},
181 {(id)kSecAttrKeyType, (id)attr},
182 {(id)kSecMatchItemList, $array((id)item)});
183 NSDictionary *attrs = $dict({(id)attr, value});
184 return check(SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attrs), @"SecItemUpdate");
187 NSData *data = [stringValue dataUsingEncoding: NSUTF8StringEncoding];
188 SecKeychainAttribute attribute = {.tag=attr, .length=data.length, .data=(void*)data.bytes};
189 SecKeychainAttributeList list = {.count=1, .attr=&attribute};
190 return check(SecKeychainItemModifyAttributesAndData((SecKeychainItemRef)item, &list, 0, NULL),
191 @"SecKeychainItemModifyAttributesAndData");
195 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr {
196 return [[self class] _setAttribute: attr ofItem: _itemRef stringValue: valueStr];
205 BOOL check(OSStatus err, NSString *what) {
207 #if !MYCRYPTO_USE_IPHONE_API
208 if (err < -2000000000)
209 return checkcssm(err,what);
211 Warn(@"MYCrypto error, %@: %@", what, MYErrorName(NSOSStatusErrorDomain,err));
217 #if !MYCRYPTO_USE_IPHONE_API
218 BOOL checkcssm(CSSM_RETURN err, NSString *what) {
219 if (err != CSSM_OK) {
220 Warn(@"MYCrypto error, %@: %@", what, MYErrorName(MYCSSMErrorDomain,err));