列出Mac OS X上的钥匙串中的条目

时间:2011-12-27 14:21:45

标签: macos keychain

过去几天我一直在玩Cocoa,我想知道如何列出我创建的钥匙串的所有名称/帐户对? Mac OS X附带的小钥匙链访问应用程序可以做到这一点,所以我认为它必须是可能的吗? SecItemCopyMatching我正在寻找什么?但是,如何指定要搜索的钥匙串?在这种情况下,什么是服务名称?

...我是唯一一个认为Cocoa中的Keychain API绝对可怕的人吗?在过去几个小时左右我一直在阅读文档,我仍然无处可去: - /

2 个答案:

答案 0 :(得分:7)

您使用SecItemCopyMatching对钥匙串中的项目进行迭代,并使用SecKeychainFindInternetPasswordSecKeychainFindGenericPassword访问密码。

迭代钥匙串

// iterates over keychain and pass every item found by the query to PrintAccount.
static void IterateOverKeychain() {
    // create query
    CFMutableDictionaryRef query = CFDictionaryCreateMutable(kCFAllocatorDefault, 3, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionaryAddValue(query, kSecReturnAttributes, kCFBooleanTrue);
    CFDictionaryAddValue(query, kSecMatchLimit, kSecMatchLimitAll);
    CFDictionaryAddValue(query, kSecClass, kSecClassInternetPassword);

    // get search results
    CFArrayRef result = nil;
    OSStatus status = SecItemCopyMatching(query, (CFTypeRef*)&result);
    assert(status == 0);

    // do something with the result
    CFRange range = CFRangeMake(0, CFArrayGetCount(result));
    CFArrayApplyFunction(result, range, PrintAccount, nil);
}

// prints the password for a item from the keychain.
static void PrintAccount(const void *value, void *context) {
    CFDictionaryRef dict = value;
    CFStringRef acct = CFDictionaryGetValue(dict, kSecAttrAccount);
    NSLog(@"%@", acct);
}

打印密码

static void PrintPassword() {
    const char *acct = "foo.bar@googlemail.com";
    UInt32 acctLen = (UInt32)strlen(acct);

    const char *srvr = "calendar.google.com";
    UInt32 srvrLen = (UInt32)strlen(srvr);

    UInt32 pwLen = 0;
    void *pw = 0;

    SecKeychainFindInternetPassword(nil, srvrLen, srvr, 0, nil, acctLen, acct, 0, nil, 0, kSecProtocolTypeAny, kSecAuthenticationTypeAny, &pwLen, &pw, nil);

    CFStringRef pwString = CFStringCreateWithBytes(kCFAllocatorDefault, pw, pwLen, kCFStringEncodingUTF8, NO);
    NSLog(@"%s %@", acct, pwString);
}

答案 1 :(得分:2)

好吧,我设法枚举了钥匙串条目,但密码字段为空。我想如果需要授权,程序会像往常一样自动询问钥匙串密码吗?

NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
                       (id)kSecClassInternetPassword, kSecClass,
                       (id)kCFBooleanTrue, kSecReturnData,
                       (id)kCFBooleanTrue, kSecReturnAttributes,
                       kSecMatchLimitAll, kSecMatchLimit,
                       nil];

NSArray *itemDicts = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&itemDicts);
if (status)
    [MessageBox Show:(NSString*)SecCopyErrorMessageString(status, NULL)];

NSMutableArray *arr = [[NSMutableArray alloc] init];

for (NSDictionary *itemDict in itemDicts) {
    NSData *data    = [itemDict objectForKey:(id)kSecValueData];
    NSString *pwd   = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    NSString *acc   = [itemDict objectForKey:(id)kSecAttrAccount];
    NSString *name  = [itemDict objectForKey:(id)kSecAttrLabel];

    if(acc != nil) {
        NSArray *values = [NSArray arrayWithObjects: (id)name, (id)acc, (id)pwd, nil];

        [arr addObject:(id)values];
    }
}
[itemDicts release];

NSInteger c     = arr.count;
NSString *cnt   = [NSString stringWithFormat:@"%d", c]; 
[MessageBox Show: [arr objectAtIndex:10]];