Code cleanup, more header comments.
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);
40 - (id) copyWithZone: (NSZone*)zone {
41 // As keys are immutable, it's not necessary to make copies of them. This makes it more efficient
42 // to use instances as NSDictionary keys or store them in NSSets.
46 - (BOOL) isEqual: (id)obj {
47 // Require the objects to be of the same class, so that a MYPublicKey will not be equal to a
48 // MYKeyPair with the same public key.
49 return (obj == self) ||
50 ([obj class] == [self class] && CFEqual(_itemRef, [obj keychainItemRef]));
54 return CFHash(_itemRef);
57 - (NSString*) description {
58 return $sprintf(@"%@[%p]", [self class], _itemRef); //FIX: Can we do anything better?
62 - (NSArray*) _itemList {
63 return $array((id)_itemRef);
67 - (CFDictionaryRef) asQuery {
68 return (CFDictionaryRef) $dict( {(id)kSecClass, (id)kSecClassKey},//FIX
69 {(id)kSecMatchItemList, self._itemList} );
74 - (MYKeychain*) keychain {
76 return [MYKeychain defaultKeychain];
78 MYKeychain *keychain = nil;
79 SecKeychainRef keychainRef = NULL;
80 if (check(SecKeychainItemCopyKeychain((SecKeychainItemRef)_itemRef, &keychainRef), @"SecKeychainItemCopyKeychain")) {
82 keychain = [[[MYKeychain alloc] initWithKeychainRef: keychainRef] autorelease];
83 CFRelease(keychainRef);
90 - (BOOL) removeFromKeychain {
92 return check(SecItemDelete(self.asQuery), @"SecItemDelete");
94 return check(SecKeychainItemDelete((SecKeychainItemRef)_itemRef), @"SecKeychainItemDelete");
100 #pragma mark DATA / METADATA ACCESSORS:
103 - (NSData*) _getContents: (OSStatus*)outError {
104 NSData *contents = nil;
109 *outError = SecKeychainItemCopyAttributesAndData(_itemRef, NULL, NULL, NULL, &length, &bytes);
110 if (!*outError && bytes) {
111 contents = [NSData dataWithBytes: bytes length: length];
112 SecKeychainItemFreeAttributesAndData(NULL, bytes);
118 + (NSData*) _getAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item {
121 NSDictionary *info = $dict( {(id)kSecClass, (id)kSecClassKey},
122 {(id)kSecMatchItemList, $array((id)item)},
123 {(id)kSecReturnAttributes, $true} );
124 CFDictionaryRef attrs;
125 if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&attrs), @"SecItemCopyMatching"))
127 CFTypeRef rawValue = CFDictionaryGetValue(attrs,attr);
128 value = rawValue ?[[(id)CFMakeCollectable(rawValue) retain] autorelease] :nil;
132 UInt32 format = kSecFormatUnknown;
133 SecKeychainAttributeInfo info = {.count=1, .tag=(UInt32*)&attr, .format=&format};
134 SecKeychainAttributeList *list = NULL;
136 if (check(SecKeychainItemCopyAttributesAndData((SecKeychainItemRef)item, &info,
137 NULL, &list, NULL, NULL),
138 @"SecKeychainItemCopyAttributesAndData")) {
140 if (list->count == 1)
141 value = [NSData dataWithBytes: list->attr->data
142 length: list->attr->length];
143 else if (list->count > 1)
144 Warn(@"Multiple values for keychain item attribute");
145 SecKeychainItemFreeAttributesAndData(list, NULL);
152 + (NSString*) _getStringAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item {
153 NSData *value = [self _getAttribute: attr ofItem: item];
154 if (!value) return nil;
155 const char *bytes = value.bytes;
156 size_t length = value.length;
157 if (length>0 && bytes[length-1] == 0)
158 length--; // Some values are null-terminated!?
159 NSString *str = [[NSString alloc] initWithBytes: bytes length: length
160 encoding: NSUTF8StringEncoding];
162 Warn(@"MYKeychainItem: Couldn't decode attr value as string");
163 return [str autorelease];
166 - (NSString*) stringValueOfAttribute: (SecKeychainAttrType)attr {
167 return [[self class] _getStringAttribute: attr ofItem: _itemRef];
171 + (BOOL) _setAttribute: (SecKeychainAttrType)attr ofItem: (MYKeychainItemRef)item
172 stringValue: (NSString*)stringValue
175 id value = stringValue ?(id)stringValue :(id)[NSNull null];
176 NSDictionary *query = $dict({(id)kSecClass, (id)kSecClassKey},
177 {(id)kSecAttrKeyType, (id)attr},
178 {(id)kSecMatchItemList, $array((id)item)});
179 NSDictionary *attrs = $dict({(id)attr, value});
180 return check(SecItemUpdate((CFDictionaryRef)query, (CFDictionaryRef)attrs), @"SecItemUpdate");
183 NSData *data = [stringValue dataUsingEncoding: NSUTF8StringEncoding];
184 SecKeychainAttribute attribute = {.tag=attr, .length=data.length, .data=(void*)data.bytes};
185 SecKeychainAttributeList list = {.count=1, .attr=&attribute};
186 return check(SecKeychainItemModifyAttributesAndData((SecKeychainItemRef)item, &list, 0, NULL),
187 @"SecKeychainItemModifyAttributesAndData");
191 - (BOOL) setValue: (NSString*)valueStr ofAttribute: (SecKeychainAttrType)attr {
192 return [[self class] _setAttribute: attr ofItem: _itemRef stringValue: valueStr];
201 BOOL check(OSStatus err, NSString *what) {
204 if (err < -2000000000)
205 return checkcssm(err,what);
207 Warn(@"MYCrypto error, %@: %@", what, MYErrorName(NSOSStatusErrorDomain,err));
214 BOOL checkcssm(CSSM_RETURN err, NSString *what) {
215 if (err != CSSM_OK) {
216 Warn(@"MYCrypto error, %@: %@", what, MYErrorName(MYCSSMErrorDomain,err));