Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:36:31+00:00 2026-05-27T10:36:31+00:00

From this question, I know you can use SecKeychainFindGenericPassword without a username value. It

  • 0

From this question, I know you can use SecKeychainFindGenericPassword without a username value. It will still return a keychain item for the given service. But how do I then get the username?
Get the Username(s) stored in Keychain, using only the ServiceName? OR: Where are you supposed to store the Username?

//SecKeychainFindGenericPassword
//Finds the first generic password based on the attributes passed.

OSStatus SecKeychainFindGenericPassword (
   CFTypeRef keychainOrArray,
   UInt32 serviceNameLength,
   const char *serviceName,
   UInt32 accountNameLength,
   const char *accountName,
   UInt32 *passwordLength,
   void **passwordData,
   SecKeychainItemRef *itemRef
);

My code looks like this:

UInt32 passwordLength = 0;
char *password = nil;

SecKeychainItemRef item = nil;

OSStatus returnStatus = SecKeychainFindGenericPassword(NULL, 9, @"MyAppName", 0, NULL, &passwordLength, (void **)&password, &item);

//Get password
NSString *passwordString = [[[NSString alloc] initWithData:[NSData dataWithBytes:password length:passwordLength] encoding:NSUTF8StringEncoding] autorelease];
SecKeychainItemFreeContent(NULL, password);

//Get username (not yet working)
UInt32 attributeTags[1];
*attributeTags = 'acct';// TODO: populate with the kSecAttrAccount constant (NOT a string cast to a UInt32)
//kSecAccountItemAttr = 'acct',

UInt32 formatConstants[1];
*formatConstants = 0; // TODO: populate with a format constant found in "cssmtype.h". I'm picking "0" = "string" in list below...

//SecKeychainAttributeInfo doc says: "A pointer to the first attribute format in the array. Attribute formats are of type CSSM_DB_ATTRIBUTE_FORMAT."

/* //found in "cssmtype.h"
 typedef uint32 CSSM_DB_ATTRIBUTE_FORMAT, *CSSM_DB_ATTRIBUTE_FORMAT_PTR;
 enum {
 CSSM_DB_ATTRIBUTE_FORMAT_STRING =          0,
 CSSM_DB_ATTRIBUTE_FORMAT_SINT32 =          1,
 CSSM_DB_ATTRIBUTE_FORMAT_UINT32 =          2,
 CSSM_DB_ATTRIBUTE_FORMAT_BIG_NUM =         3,
 CSSM_DB_ATTRIBUTE_FORMAT_REAL =                4,
 CSSM_DB_ATTRIBUTE_FORMAT_TIME_DATE =       5,
 CSSM_DB_ATTRIBUTE_FORMAT_BLOB =                6,
 CSSM_DB_ATTRIBUTE_FORMAT_MULTI_UINT32 =        7,
 CSSM_DB_ATTRIBUTE_FORMAT_COMPLEX =         8
 };
*/

struct SecKeychainAttributeInfo attributeInfo, 1, *attributeTags, *formatConstants; //ERROR: "Expected ';' at end of declaration list.
//OR:
/*
struct SecKeychainAttributeInfo
{
    UInt32 1; //ERROR: "Expected ';' at end of declaration list.
    UInt32 *attributeTags;
    UInt32 *formatConstants;
}attributeInfo;
*/

/*
SecKeychainAttributeInfo *attributeInfo; //TODO: "change it to hold the structure directly"
attributeInfo->count = 1;
attributeInfo->tag = attributeTags;
attributeInfo->format = formatConstants;
*/

SecKeychainAttributeList *attributeList = nil;
OSStatus attributeStatus = SecKeychainItemCopyAttributesAndData(item, &attributeInfo, NULL, &attributeList, 0, NULL);

if (attributeStatus != noErr)
{
    if (_logsErrors)
        NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(attributeStatus));
    return nil;
}


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

How do I get an NSString value of the username contained in that SecKeychainItemRef?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:36:32+00:00Added an answer on May 27, 2026 at 10:36 am
    + (EMGenericKeychainItem *)genericKeychainItemForService:(NSString *)serviceName
    {
        if (!serviceName)
            return nil;
    
        const char *serviceNameCString = [serviceName UTF8String];
    
        UInt32 passwordLength = 0;
        char *password = nil;
    
        SecKeychainItemRef item = nil;
        OSStatus returnStatus = SecKeychainFindGenericPassword(NULL, strlen(serviceNameCString), serviceNameCString, 0, NULL, &passwordLength, (void **)&password, &item);
    
        UInt32 attributeTags[1];
        *attributeTags = kSecAccountItemAttr;
    
        UInt32 formatConstants[1];
        *formatConstants = CSSM_DB_ATTRIBUTE_FORMAT_STRING; //From "cssmtype.h" under "CSSM_DB_ATTRIBUTE_FORMAT".
    
        struct SecKeychainAttributeInfo
        {
            UInt32 count;
            UInt32 *tag;
            UInt32 *format;
        }attributeInfo;
    
        attributeInfo.count = 1;
        attributeInfo.tag = attributeTags;
        attributeInfo.format = formatConstants;
    
        SecKeychainAttributeList *attributeList = nil;
        OSStatus attributeStatus = SecKeychainItemCopyAttributesAndData(item, &attributeInfo, NULL, &attributeList, 0, NULL);
    
        if (attributeStatus != noErr || !item)
        {
            if (_logsErrors)
                NSLog(@"Error (%@) - %s", NSStringFromSelector(_cmd), GetMacOSStatusErrorString(returnStatus));
            return nil;
        }
    
        SecKeychainAttribute accountNameAttribute = attributeList->attr[0];
    
        NSString* accountName = [[[NSString alloc] initWithData:[NSData dataWithBytes:accountNameAttribute.data length:accountNameAttribute.length] encoding:NSUTF8StringEncoding] autorelease];
    
        NSString *passwordString = [[[NSString alloc] initWithData:[NSData dataWithBytes:password length:passwordLength] encoding:NSUTF8StringEncoding] autorelease];
        SecKeychainItemFreeContent(NULL, password);         
    
        return [EMGenericKeychainItem _genericKeychainItemWithCoreKeychainItem:item forServiceName:serviceName username:accountName password:passwordString];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know from this question that Firefox 3.0 and up stores its cookies in
Following on from this question , I now want to know how to stop
From this question System.currentTimeMillis() is not accurate on windows XP? I know that on
I know since the 3.0 SDK we can use accessories, so my question is
This question has been asked before - How we can use CTE in subquery
From this question , a neat answer about using COALESCE to simplify complex logic
From this question I learned Double.NaN is not equal to itself. I was verifying
Coming from this question , I have a wxComboCtrl with a custom popup made
Continuing from this question : When I am trying to do fopen on Windows,
This question follows on from this vim search question I have a setting in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.