1.1 --- a/MYKey-iPhone.m Tue Jun 09 23:58:03 2009 -0700
1.2 +++ b/MYKey-iPhone.m Tue Jul 21 10:13:08 2009 -0700
1.3 @@ -19,45 +19,6 @@
1.4 @implementation MYKey
1.5
1.6
1.7 -+ (SecKeyRef) _addKeyWithInfo: (NSMutableDictionary*)info {
1.8 - if (![info objectForKey: (id)kSecAttrApplicationTag]) {
1.9 - // Every keychain item has to have a unique tag, apparently, or you'll get spurious
1.10 - // duplicate-item errors. If none was given, make up a random one:
1.11 - UInt8 tag[16];
1.12 - Assert(check(SecRandomCopyBytes(kSecRandomDefault, sizeof(tag), tag), @"SecRandomCopyBytes"));
1.13 - [info setObject: [NSData dataWithBytes: tag length: sizeof(tag)]
1.14 - forKey: (id)kSecAttrApplicationTag];
1.15 - }
1.16 - CFDataRef keyPersistentRef;
1.17 - SecKeyRef key;
1.18 - OSStatus err = SecItemAdd((CFDictionaryRef)info, (CFTypeRef*)&keyPersistentRef);
1.19 - if (err==errSecDuplicateItem) {
1.20 - // it's already in the keychain -- get a reference to it:
1.21 - [info removeObjectForKey: (id)kSecReturnPersistentRef];
1.22 - [info setObject: $true forKey: (id)kSecReturnRef];
1.23 - if (check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef *)&key),
1.24 - @"SecItemCopyMatching"))
1.25 - return key;
1.26 - } else if (check(err, @"SecItemAdd")) {
1.27 - // It was added
1.28 - if ([[info objectForKey: (id)kSecReturnPersistentRef] boolValue]) {
1.29 - // now get its SecKeyRef:
1.30 - info = $mdict({(id)kSecValuePersistentRef, (id)keyPersistentRef},
1.31 - {(id)kSecReturnRef, $true});
1.32 - err = SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef *)&key);
1.33 - CFRelease(keyPersistentRef);
1.34 - if (check(err,@"SecItemCopyMatching")) {
1.35 - Assert(key!=nil);
1.36 - return key;
1.37 - }
1.38 - } else {
1.39 - return (SecKeyRef)keyPersistentRef;
1.40 - }
1.41 - }
1.42 - return NULL;
1.43 -}
1.44 -
1.45 -
1.46 - (id) initWithKeyRef: (SecKeyRef)key {
1.47 return [self initWithKeychainItemRef: (SecKeychainItemRef)key];
1.48 }
1.49 @@ -70,8 +31,8 @@
1.50 {(id)kSecAttrKeyType, (id)self.keyType},
1.51 {(id)kSecValueData, data},
1.52 {(id)kSecAttrIsPermanent, (keychain ?$true :$false)},
1.53 - {(id)kSecReturnPersistentRef, $true} );
1.54 - SecKeyRef key = [[self class] _addKeyWithInfo: info];
1.55 + {(id)kSecReturnPersistentRef, (keychain ?$true :$false)} );
1.56 + SecKeyRef key = (SecKeyRef)[MYKeychain _addItemWithInfo: info];
1.57 if (!key) {
1.58 [self release];
1.59 return nil;
1.60 @@ -80,6 +41,9 @@
1.61 if (self) {
1.62 if (!keychain)
1.63 _keyData = [data copy];
1.64 +
1.65 + //TEMP For debugging:
1.66 + AssertEqual(self.keyData, data);
1.67 }
1.68 return self;
1.69 }
1.70 @@ -95,6 +59,19 @@
1.71 }
1.72
1.73
1.74 +/*- (NSData*) persistentRef {
1.75 + NSDictionary *info = $dict( {(id)kSecValueRef, (id)self.keyRef},
1.76 + //{(id)kSecAttrIsPermanent, (self.isPersistent ?$true :$false)},
1.77 + {(id)kSecReturnPersistentRef, $true} );
1.78 + CFDataRef data;
1.79 + if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&data), @"SecItemCopyMatching"))
1.80 + return nil;
1.81 + if (!data)
1.82 + Warn(@"MYKey persistentRef couldn't get ref");
1.83 + return [NSMakeCollectable(data) autorelease];
1.84 +}*/
1.85 +
1.86 +
1.87 - (SecExternalItemType) keyClass {
1.88 AssertAbstractMethod();
1.89 }
1.90 @@ -108,11 +85,13 @@
1.91 return _keyData;
1.92
1.93 NSDictionary *info = $dict( {(id)kSecValueRef, (id)self.keyRef},
1.94 + //{(id)kSecAttrIsPermanent, (self.isPersistent ?$true :$false)},
1.95 {(id)kSecReturnData, $true} );
1.96 CFDataRef data;
1.97 - if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&data), @"SecItemCopyMatching"))
1.98 + if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&data), @"SecItemCopyMatching")) {
1.99 + Log(@"SecItemCopyMatching failed; input = %@", info);
1.100 return nil;
1.101 - else {
1.102 + } else {
1.103 Assert(data!=NULL);
1.104 _keyData = NSMakeCollectable(data);
1.105 return _keyData;
1.106 @@ -134,11 +113,11 @@
1.107
1.108 - (id) _attribute: (CFTypeRef)attribute {
1.109 NSDictionary *info = $dict({(id)kSecValueRef, (id)self.keyRef},
1.110 + {(id)kSecAttrIsPermanent, (self.isPersistent ?$true :$false)},
1.111 {(id)kSecReturnAttributes, $true});
1.112 CFDictionaryRef attrs = NULL;
1.113 if (!check(SecItemCopyMatching((CFDictionaryRef)info, (CFTypeRef*)&attrs), @"SecItemCopyMatching"))
1.114 return nil;
1.115 - Log(@"_attribute: %@ of %@ %p", attribute, [self class], self.keyRef);//TEMP
1.116 CFTypeRef rawValue = CFDictionaryGetValue(attrs,attribute);
1.117 id value = rawValue ?[[(id)CFMakeCollectable(rawValue) retain] autorelease] :nil;
1.118 CFRelease(attrs);