区分Mac OS X钥匙串中的会话和系统项

时间:2013-08-06 09:48:32

标签: objective-c macos cocoa keychain sskeychain

如何查询Mac OS X钥匙串以仅检索会话项?

当我尝试获取所有这样的项目时:

[SSKeychain accountsForService:nil];

会话项和系统项具有完全相同的键属性:acct,cdat,class,crtr,labl,mdat,svce

如何查询钥匙串以仅获取会话项目或区分它们?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您可以像这样查询会话密码(如果您需要所有密码,请将kSecMatchLimitOne替换为kSecMatchLimitAllattributes在这种情况下将CFArrayRef类型):< / p>

NSDictionary* passwordQuery = @{
    (__bridge id)kSecAttrService : sessionServiceName,
    (__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
    (__bridge id)kSecReturnAttributes: (__bridge id)kCFBooleanTrue,
    (__bridge id)kSecReturnData: (__bridge id)kCFBooleanTrue,
    (__bridge id)kSecMatchLimit : (__bridge id)kSecMatchLimitOne
};

CFTypeRef attributes = nil;
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)passwordQuery, &attributes);
if (status != 0){
    return nil;
 }

答案 1 :(得分:0)

我通过打开用户钥匙串找到了:

NSArray *path = [NSHomeDirectory() pathComponents];
NSString *keychainPath = [NSString stringWithFormat:@"%@%@/%@%@",path[0],path[1],path[2],@"/Library/Keychains/login.keychain"];
SecKeychainRef ref = NULL;
SecKeychainOpen([keychainPath UTF8String],&ref);