2010-05-05 83 views
6

如何將安全身份(證書+私鑰)添加到iPhone鑰匙串? 我在應用程序中有.p12文件。我可以用SecPKCS12Import(),但 從它那裏得到認同,當我嘗試做到以下幾點:如何將安全標識(證書+私鑰)添加到iPhone鑰匙串?

NSMutableDictionary *secIdentityParams = [[NSMutableDictionary alloc] init];  
[secIdentityParams setObject:(id)kSecClassIdentity forKey:(id)kSecClass]; 
[secIdentityParams setObject:label forKey:(id)kSecAttrLabel]; 
[secIdentityParams setObject:(id)myIdentity forKey:(id)kSecValueRef]; 

status = SecItemAdd((CFDictionaryRef) secIdentityParams, NULL); 

我收到錯誤= -25291 - >不信任的結果是可用的。 我在做什麼錯?

回答

4

只需使用1個參數屬性字典添加身份到鑰匙串:

NSMutableDictionary *secIdentityParams = [[NSMutableDictionary alloc] init];  
[secIdentityParams setObject:(id)myIdentity forKey:(id)kSecValueRef]; 
OSStatus status = SecItemAdd((CFDictionaryRef) secIdentityParams, NULL); 
+0

OMG,就是這樣,沒有把類屬性放在它的工作。爲什麼O爲什麼安全框架如此複雜,記錄不完整和挑剔?浪費時間太多了。 – 2016-06-09 16:25:28

2

使用kSecValueRef作爲唯一的參數完美。你知道爲什麼當其他參數,如kSecClass,提供?所述鑰匙扣服務參考記載的SecItemAdd()第一參數如下:

含有項目類 鍵 - 值對[...]和任選的 屬性詞典鍵 - 值對[...] 指定項目的屬性值 值。

我假定kSecClass是一個強制性的參數,它必須始終存在,或者使用SecItemAdd()奧德SecItemCopyMatching()時。 Certificate, Key, and Trust Services Tasks on iOS解釋加入到SecIdentityRef如下鑰匙扣(清單2-3)的過程:

CFDataRef persistentRefForIdentity(SecIdentityRef identity) 
{ 
    OSStatus status; 

    CFTypeRef identity_handle = NULL; 
    const void *keys[] = { kSecReturnPersistentRef, kSecValueRef }; 
    const void *values[] = { kCFBooleanTrue,   identity }; 
    CFDictionaryRef dict = CFDictionaryCreate(NULL, keys, values, 
               2, NULL, NULL); 
    status = SecItemAdd(dict, &persistent_ref); 

    if (dict) 
     CFRelease(dict); 

    return (CFDataRef)persistent_ref; 
} 

是這個例子只是錯了嗎?

1

當通過SecItemAdd()添加新的SecIdentityRef時,我設法讓鑰匙串服務返回持久鑰匙串參考。這裏是工作代碼:

- (NSData *)persistentKeychainReferenceForIdentity:(SecIdentityRef)identity 
{ 
    NSData *persistentRef = nil; 
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
           (id)identity, kSecValueRef, 
           (id)kCFBooleanTrue, kSecReturnPersistentRef, 
           nil]; 
    OSStatus itemAddStatus = SecItemAdd((CFDictionaryRef)attributes, 
             (CFTypeRef *)&persistentRef); 
    if (itemAddStatus != errSecSuccess) 
    { 
     return nil; 
    }  

    return persistentRef; 
} 

我希望這也可以幫助其他人。

+0

你對這個持久性引用做了什麼?將其存儲在用戶偏好中以便稍後訪問,然後從那裏查找鑰匙串? – lostintranslation 2015-06-02 14:49:32