将私钥添加到iOS Keychain中

时间:2012-07-26 13:42:59

标签: ios objective-c keychain private-key

我正在尝试将一个私钥添加到iOS钥匙串中。证书(公钥)工作正常,但私钥拒绝...我完全搞砸为什么下面的代码不起作用。

首先,我检查钥匙串中的当前密钥(= Keychain是密钥/值存储的密钥)是否“空闲”。然后我要添加私钥。

CFStringRef labelstring = CFStringCreateWithCString(NULL, [key cStringUsingEncoding:NSUTF8StringEncoding], kCFStringEncodingUTF8);

NSArray* keys = [NSArray arrayWithObjects:(__bridge id)kSecClass,kSecAttrLabel,kSecReturnData,kSecAttrAccessible,nil];
NSArray* values = [NSArray arrayWithObjects:(__bridge id)kSecClassKey,labelstring,kCFBooleanTrue,kSecAttrAccessibleWhenUnlocked,nil];
NSMutableDictionary* searchdict = [NSMutableDictionary dictionaryWithObjects:values forKeys:keys];

CFRelease(labelstring);

NSMutableDictionary *query = searchdict;


CFTypeRef item = NULL;
OSStatus error = SecItemCopyMatching((__bridge_retained CFDictionaryRef) query, &item);

if (error)
{
    NSLog(@"Error: %ld (statuscode)", error);
}

if(error != errSecItemNotFound)
{
    SecItemDelete((__bridge_retained CFDictionaryRef) query);
}

[query setObject:(id)data forKey:(__bridge id)kSecValueData];

OSStatus status = SecItemAdd((__bridge_retained CFDictionaryRef) query, &item);

if(status)
{
    NSLog(@"Keychain error occured: %ld (statuscode)", status);
    return NO;
}

调试输出如下:

2012-07-26 15:33:03.772 App[15529:1b03] Error: -25300 (statuscode)
2012-07-26 15:33:11.195 App[15529:1b03] Keychain error occured: -25299 (statuscode)

第一个错误代码-25300代表errSecItemNotFound。因此,此密钥没有存储值。然后,当我尝试将私钥添加到钥匙串中时,我得到-25299,这意味着errSecDuplicateItem。我不明白。为什么会这样?

有没有人对此有任何线索或暗示?

Apple的错误代码:

errSecSuccess                = 0,       /* No error. */
errSecUnimplemented          = -4,      /* Function or operation not implemented. */
errSecParam                  = -50,     /* One or more parameters passed to a function where not valid. */
errSecAllocate               = -108,    /* Failed to allocate memory. */
errSecNotAvailable           = -25291,  /* No keychain is available. You may need to restart your computer. */
errSecDuplicateItem          = -25299,  /* The specified item already exists in the keychain. */
errSecItemNotFound           = -25300,  /* The specified item could not be found in the keychain. */
errSecInteractionNotAllowed  = -25308,  /* User interaction is not allowed. */
errSecDecode                 = -26275,  /* Unable to decode the provided data. */
errSecAuthFailed             = -25293,  /* The user name or passphrase you entered is not correct. */ 

提前致谢!

更新#1:我发现它只是第一次有效。即使数据和密钥不同,在第一次存储到钥匙串后,我也无法存储更多的密钥。

2 个答案:

答案 0 :(得分:8)

以下代码对我有用:

NSMutableDictionary *query = [[NSMutableDictionary alloc] init]; 
[query setObject:(id)kSecClassKey forKey:(id)kSecClass]; 
[query setObject:(id)kSecAttrAccessibleWhenUnlocked forKey:(id)kSecAttrAccessible]; 
[query setObject:[NSNumber numberWithBool:YES] forKey:(id)kSecReturnData];

//adding access key 
[query setObject:(id)key forKey:(id)kSecAttrApplicationTag];


//removing item if it exists 
SecItemDelete((CFDictionaryRef)query);

//setting data (private key) 
[query setObject:(id)data forKey:(id)kSecValueData];

CFTypeRef persistKey; OSStatus status = SecItemAdd((CFDictionaryRef)query, &persistKey);

if(status) {
    NSLog(@"Keychain error occured: %ld (statuscode)", status);
    return NO; 
}

答案 1 :(得分:1)

很抱歉,但我永远无法调试您的代码。 Apple提供了一些示例代码(KeychainItemWrapper),可以保存一个字符串(我记得)。它是处理钥匙链的一大帮助。 Web上有一个要点,即该类的修改版本,但保存并恢复字典(存档为数据对象,这是Apple代码对字符串的作用)。这使您可以将一个界面中的多个项目保存到钥匙串中。要点在这里Keychain for NSDictionary/data