Distinguish session and system items in Mac OS X keychain

487 views Asked by At

How can I query the Mac OS X keychain to retrieve only session items ?

When I try to get all items like this :

[SSKeychain accountsForService:nil];

A session item and a system item have exactly the same attributes for the keys : acct, cdat, class, crtr, labl, mdat, svce

How can y query the keychain to get only session items or distinguish them ?

Thank you for your help !

2

There are 2 answers

0
Michaël On BEST ANSWER

I have found by opening the user keychain :

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);
3
Sergiy Salyuk On

You can query the session password like this (replace kSecMatchLimitOne with kSecMatchLimitAll if you need all passwords. The attributes will have CFArrayRef type in this case):

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