Retrieving keyring element attributes

I am trying to get the attributes of a keychain element. This code should search for all available attributes, then print their tags and contents.

According to docs , I should see tags like 'cdat', but instead they just look like an index (i.e. the first tag is 0, then 1). This makes it rather useless, since I cannot determine which attribute is the one I'm looking for.

    SecItemClass itemClass;
    SecKeychainItemCopyAttributesAndData(itemRef, NULL, &itemClass, NULL, NULL, NULL);

    SecKeychainRef keychainRef;
    SecKeychainItemCopyKeychain(itemRef, &keychainRef);

    SecKeychainAttributeInfo *attrInfo;
    SecKeychainAttributeInfoForItemID(keychainRef, itemClass, &attrInfo);

    SecKeychainAttributeList *attributes;
    SecKeychainItemCopyAttributesAndData(itemRef, attrInfo, NULL, &attributes, 0, NULL);

    for (int i = 0; i < attributes->count; i ++)
    {
        SecKeychainAttribute attr = attributes->attr[i];
        NSLog(@"%08x %@", attr.tag, [NSData dataWithBytes:attr.data length:attr.length]);
    }

    SecKeychainFreeAttributeInfo(attrInfo);
    SecKeychainItemFreeAttributesAndData(attributes, NULL);
    CFRelease(itemRef);
    CFRelease(keychainRef);
+5
source share
2 answers

There are two things you should do here. First, before calling SecKeychainAttributeInfoForItemID you need to handle the "general" itemClasses ...

switch (itemClass)
{
    case kSecInternetPasswordItemClass:
        itemClass = CSSM_DL_DB_RECORD_INTERNET_PASSWORD;
        break;
    case kSecGenericPasswordItemClass:
        itemClass = CSSM_DL_DB_RECORD_GENERIC_PASSWORD;
        break;
    case kSecAppleSharePasswordItemClass:
        itemClass = CSSM_DL_DB_RECORD_APPLESHARE_PASSWORD;
        break;
    default:
        // No action required
}

-, attr.tag FourCharCode , ..

NSLog(@"%c%c%c%c %@",
    ((char *)&attr.tag)[3],
    ((char *)&attr.tag)[2],
    ((char *)&attr.tag)[1],
    ((char *)&attr.tag)[0],
    [[[NSString alloc]
        initWithData:[NSData dataWithBytes:attr.data length:attr.length]
        encoding:NSUTF8StringEncoding]
    autorelease]]);

, - UTF8 .

+3

, .

, , keychain .

SecKeychainItemCopyAttributesAndData SecKeychainAttributeList, SecKeychainAttributes. TFD:

4- . . " " .

( " " ) 4 char, .

+1

All Articles