相关疑难解决方法(0)

iOS Keychain Services:只允许kSecAttrGeneric Key的特定值?

我正在尝试使用此Apple示例代码中提供的KeychainWrapper类:https://developer.apple.com/library/content/samplecode/GenericKeychain/

在示例应用程序中,该类具有以下开始的init方法:

- (id)initWithIdentifier: (NSString *)identifier accessGroup:(NSString *) accessGroup;
{
    if (self = [super init])
    {
        // Begin Keychain search setup. The genericPasswordQuery leverages the special user
        // defined attribute kSecAttrGeneric to distinguish itself between other generic Keychain
        // items which may be included by the same application.
        genericPasswordQuery = [[NSMutableDictionary alloc] init];

        [genericPasswordQuery setObject:(id)kSecClassGenericPassword forKey:(id)kSecClass];
        [genericPasswordQuery setObject:identifier forKey:(id)kSecAttrGeneric];
Run Code Online (Sandbox Code Playgroud)

在示例应用程序中,它使用两个值作为标识符字符串."密码"和"帐号".在我的代码中实现类时,我使用了一些自定义标识符,但代码不起作用.对SecItemAdd()的调用失败.经过一些测试后,似乎使用"密码"和"帐号"以外的值作为标识符不起作用.

有谁知道允许的值和/或是否可以为您的钥匙串项目设置自定义标识符?

keychain ios

38
推荐指数
2
解决办法
3万
查看次数

将私钥添加到iOS Keychain中

我正在尝试将一个私钥添加到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 …
Run Code Online (Sandbox Code Playgroud)

objective-c keychain ios private-key

20
推荐指数
1
解决办法
2万
查看次数

标签 统计

ios ×2

keychain ×2

objective-c ×1

private-key ×1