gpt4 book ai didi

ios - 将私钥添加到 iOS Keychain

转载 作者:IT王子 更新时间:2023-10-29 08:06:40 29 4
gpt4 key购买 nike

我正在尝试将私钥添加到 iOS 钥匙串(keychain)中。证书(公钥)工作正常但私钥拒绝...我完全不明白为什么以下代码不起作用。

首先,我检查当前 key (=key,如果钥匙串(keychain)是一个键/值存储)在钥匙串(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。所以没有为这个键存储任何值。然后,当我尝试将私钥添加到钥匙串(keychain)中时,我得到 -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:我发现它只在第一次起作用。即使数据和 key 不同,在第一次存储到钥匙串(keychain)后我也无法存储更多 key 。

最佳答案

以下代码对我有用:

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;
}

关于ios - 将私钥添加到 iOS Keychain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670647/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com