5

キーチェーン アイテムの属性を取得しようとしています。このコードは、利用可能なすべての属性を検索し、それらのタグとコンテンツを出力する必要があります。

ドキュメントによると、 「cdat」のようなタグが表示されるはずですが、代わりにインデックスのように見えます (つまり、最初のタグは 0、次は 1 です)。これは、探している属性がどれか分からないため、かなり役に立ちません。

    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);
4

2 に答える 2

3

ここで行うべきことは 2 つあります。まず、SecKeychainAttributeInfoForItemID への呼び出しの前に、「一般的な」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 でエンコードされたデータです。

于 2010-03-26T01:37:41.277 に答える
1

ドキュメントは少し混乱を招くと思います。

私が見ている数字は、キーのキーチェーン アイテム属性定数のようです

ただし、SecKeychainItemCopyAttributesAndData は、SecKeychainAttributes の配列を含む SecKeychainAttributeList 構造体を返します。TFD より:

tag 4 バイトの属性タグ。有効な属性タイプについては、「キーチェーン アイテム属性定数」を参照してください。

属性定数 (「for キー」以外の種類) は、私が期待していた 4 文字の値です。

于 2009-07-27T04:57:34.033 に答える