2011-03-01 33 views
0

我编写了一些代码来测试将项目添加到钥匙串。我正在测试iPad 4.2.1(越狱)。我在iPad上与ldid -S prog签署了二进制文件。iOS错误errSecInteractionNotAllowed或SecItemAdd上的-25308

代码:

#import <Security/Security.h> 
#import <Security/SecItem.h> 
#import <Foundation/NSDictionary.h> 
#import <Foundation/NSString.h> 
#import <Foundation/NSObject.h> 
#import <CoreFoundation/CoreFoundation.h> 
#import <Foundation/NSAutoreleasePool.h> 
#import <Foundation/NSKeyValueCoding.h> 

int main(int argc, char *argv[]) 
    { 
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
     NSDictionary *attributesToStore = [NSDictionary dictionaryWithObjectsAndKeys: 
     [@"testuser01" dataUsingEncoding:NSUTF8StringEncoding],kSecAttrAccount, 
     [@"test123" dataUsingEncoding:NSUTF8StringEncoding],kSecValueData, 
     kSecClassInternetPassword,kSecClass, 
     [@"www.example.com" dataUsingEncoding:NSUTF8StringEncoding],kSecAttrServer, 
     kCFBooleanTrue, kSecReturnPersistentRef, 
     [@"Sample password" dataUsingEncoding:NSUTF8StringEncoding], kSecAttrDescription, 
     [@"password label" dataUsingEncoding:NSUTF8StringEncoding],kSecAttrLabel, nil]; 
     NSData *persistentRef = nil; 
     OSStatus result = SecItemAdd((CFDictionaryRef)attributesToStore, (CFTypeRef *)&persistentRef); 
     if (noErr == result) 
       { 
       NSLog(@"Added item to Keychain"); 
       } 
     else { 
       NSLog(@"Item add failed"); 
       NSLog(@"Result code: %d",result); 
      }  
     [pool release]; 
     return 0; 
    } 

代码编译,没有任何噪音或警告的链接。但在iPad上执行会引发错误-25308

如何解决此错误?

回答

0

我很肯定你需要设置kSecClass键,这样钥匙串就知道你想要添加什么样的项目。

作为一个方面说明,我发现后,我重写了init方法的GenericKeychain示例代码是有用的,如我的回答概括了我的问题here.

+0

感谢您的回应,西蒙。我正在查看您的代码,但上面的代码确实将“kSecClass”设置为kSecClassInternetPassword,kSecClass, sandflow

0

与示例代码的主要问题是,许多项目编码作为应使用NSString对象的NSData对象(kSecAttrAccount,kSecAttrLabel,kSecAttrDescription和kSecAttrServer)。我很惊讶这个问题不会导致异常,尽管iOS上的行为可能与Lion不同(我在这里看到的)。

也可能是指定kSecReturnRef而不是kSecReturnPersistentRef可能更合适(从文档中,使用kSecReturnPersistentRef发布“持久引用可能存储在磁盘上或在进程之间传递”)。这是一种指定钥匙串项目以便与SecItemUpdate,SecItemDelete或SecItemCopyMatching一起使用的方法,它使用kSecMatchItemList,它具有会话间持久性(例如使用NSUserDefaults)或传递给其他进程的优点。如果该项目仅在应用程序的生命周期内使用,或者更适合使用其他属性,那么使用kSecReturnRef的项目引用可能更合适。