2009-07-27 2 views
5

키 체인 항목의 속성을 가져 오려고합니다. 이 코드는 사용 가능한 모든 속성을 찾아보고 태그와 내용을 인쇄합니다.키 체인 항목의 속성 가져 오기

the docs에 따르면 '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); 

답변

1

설명서가 다소 혼란 스럽습니다.

숫자는 keychain item attribute constants for keys 인 것으로 보입니다.

그러나 SecKeychainItemCopyAttributesAndData는 SecKeychainAttributeList 배열을 포함하는 SecKeychainAttributeList 구조체를 반환합니다. TFD에서 :

태그 4 바이트 특성 태그. 유효한 속성 유형은 "키 체인 항목 속성 상수"를 참조하십시오.

("for keys"가 아닌 다양한) 속성 상수는 예상 한 4-char 값입니다.

3

여기서해야 할 두 가지 사항이 있습니다. 첫째, 당신이 ... SecKeychainAttributeInfoForItemID 호출하기 전에

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 
} 

두 번째를 "일반"itemClasses을 처리 할 필요가, 당신은 즉, 문자열로 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]]); 

을 attr.tag 변환해야 데이터를 문자열로 출력했습니다. 거의 항상 UTF8로 인코딩 된 데이터입니다.

+0

일반 항목 클래스를 처리하는 방법을 알려 주셔서 감사합니다. 'SecKeychainAttributeInfoForItemID'에 대한 문서는 많이 필요합니다. –