2009-07-27 12 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 संरचना देता है, जिसमें SecKeychainAttributes की एक सरणी होती है। टीएफडी से:

टैग एक 4-बाइट विशेषता टैग। वैध विशेषता प्रकारों के लिए "कीचेन आइटम विशेषता कॉन्स्टेंट" देखें।

विशेषता स्थिरांक (गैर- "कुंजी के लिए" विविधता) 4-चार मान हैं जिन्हें मैं देखने की उम्मीद करता हूं।

3

आपको दो चीजें यहां करनी चाहिए। सबसे पहले, आपको 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 
} 

दूसरा जरूरत है, तो आप

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

एक स्ट्रिंग के लिए एक FourCharCode से attr.tag कन्वर्ट करने के लिए, यानी की जरूरत है ध्यान दें कि मैंने डेटा को स्ट्रिंग के रूप में भी आउटपुट किया है - यह लगभग हमेशा यूटीएफ 8 एन्कोडेड डेटा होता है।

+0

जेनेरिक आइटम कक्षाओं को संभालने के तरीके के बारे में बताने के लिए धन्यवाद। 'SecKeychainAttributeInfoForItemID' के लिए प्रलेखन वांछित होने के लिए बहुत कुछ छोड़ देता है। –

संबंधित मुद्दे