从“SecKeychainFindGenericPassword”给出的“SecKeychainItemRef”中提取“用户名”?

2023-12-07

从这个问题,我知道你可以使用SecKeychainFindGenericPassword without用户名值。它仍然会返回给定服务的钥匙串项。但是我如何获取用户名呢?仅使用服务名称获取存储在钥匙串中的用户名?或者:您应该在哪里存储用户名?

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

我的代码如下所示:

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

如何获取其中包含的用户名的 NSString 值SecKeychainItemRef?


+ (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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从“SecKeychainFindGenericPassword”给出的“SecKeychainItemRef”中提取“用户名”? 的相关文章

  • 循环缓冲区录音 iOS:可能吗?

    我的一个客户想要连续录制音频 当他单击 提交 时 他只想提交最后 10 秒的内容 所以他想要连续记录并且只保留最后 x 秒 我认为这需要类似循环缓冲区的东西 但是 作为 iOS 的新手 它看起来像AVAudioRecorder只能写入文件
  • iOS:调用 Objective-C 方法的处理开销是多少?

    我正在编写一些实时音频处理代码 该代码将在音频单元的渲染回调中执行 该线程处于系统识别的最高优先级 Apple 指示最大限度地减少此调用中进行的处理量 他们的建议之一是避免 Objective C 方法调用 But why 调用 Objec
  • 为 Linux 编译 Objective-C 应用程序(API 覆盖范围)

    我可能在这里问一些奇怪的问题 但我不确定从哪里开始 问题是我正在考虑使用 Obj C 和 Foundation 类在 Mac 上编写一个命令行工具 但存在一个非常大的风险 那就是我希望能够为不同的 Linux 发行版编译它 以便将来作为服务
  • 保存录制的 AVAudioRecorder 声音文件:现在怎么办? (iOS、Xcode 4)

    在我的应用程序中 我希望用户能够录制一个声音文件并播放它 然后保存该声音文件以供以后使用 我用了本教程 http www techotopia com index php Recording Audio on an iPhone with
  • NSHTTPCookieStorage 是否跨应用程序持续存在?

    所以我只是好奇 NSHTTPCookieStorage 是否跨应用程序持久存在 或者仅在当前应用程序本地 我希望在另一个应用程序中收集的一些 cookie 可以在搜索应用程序中访问 是这样的吗 谢谢 PS 这是在 iPhone 或 iPad
  • NSMutableArray 实例变量内存管理

    我正在做最后一点内存管理整理 但有些东西我不明白 我已经检查了所有文档 Stack Overflow 等 但仍然不明白 我怀疑这与数组有关 我有一个NSMutableArray作为实例变量 我用它来保存从另一个数组中的对象创建的对象 vie
  • 如何建立辅助NSSortDescriptor排序键?

    我已成功按排序键对数据进行排序lastName 但我想知道如何排序lastName 然后由firstName 这是我用来排序的代码lastName NSSortDescriptor sortDescriptor NSSortDescript
  • NSDictionary 上的 NSPredicate

    我试图根据字母表在表格视图中创建部分 并在这些部分下按字母顺序对我的条目进行排序 我已经收集了 bandArrayIndex 中 bandArray 每个条目的第一个字母 现在我尝试使用 NSPredicate 来计算每个字母有多少个 我正
  • 最近打开的应用程序[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 有什么方法可以获取最近打开的应用程序 例如 4 个 的列表吗 如果是这样 怎么办 可可麦克 看看LaunchServices LSSh
  • iPad 照片选择器崩溃

    我正在使用以下函数根据 UIActionSheet 的结果激活设备相机或图像选择器 如果 fromCamera YES 那么它适用于 iPhone 和 iPad 如果 fromCamera NO 那么它可以在 iPhone 上运行并出现图像
  • 您是否标记 UIView 或将它们保留为属性?

    这主要是一个风格问题 但自从我开始为 iPhone 编程以来 我一直很好奇其他人的想法是什么 当您的 iPhone 应用程序中有一个 UIView 并且需要在应用程序的其他位置访问它时 通常在视图控制器中的另一个函数中 您是否喜欢用整数标记
  • 最小的 iOS 蓝牙管理器示例

    我一直在构建一个最小的示例 用于使用 iOS 5 0 中的 BluetoothManager 私有框架来检测附近的蓝牙设备 使用此问题中找到的答案 寻找触手可及的通用蓝牙设备 https stackoverflow com question
  • NSSharingService 共享子菜单

    如何在 Mac 应用程序中添加共享子菜单 例如 Safari gt 文件 gt 共享 我戳了戳Apple 共享服务示例代码 http developer apple com library mac samplecode SharingSer
  • 在 Objective-C 中比较 2 个字符串

    我写了以下代码 if depSelectedIndice gt 1 comSelectedIndice gt 1 NSLog depart elemet d depSelectedIndice NSLog depart elemet d c
  • 强制 Apache HTTPD 以 32 位运行

    我通过从二进制文件 以及 ppc 部分 中剥离 32 位架构 以 64 位模式运行 Apache HTTPD 我这样做是为了使其与 python 和 mysql 更加兼容 然而 我有另一台机器需要它以 32 位模式运行 它仍然保留所有四种原
  • NSString – 静态还是内联?有性能提升吗?

    如果我写的话会有任何性能提升吗 NSString helloStringWithName NSString name static NSString formatString Hello return NSString stringWith
  • 使用 iPhone 中的地图视图读取当前位置名称

    我读取了当前位置的纬度和经度值 然后成功将该位置固定在 iPhone 中 现在我想使用这个纬度和经度值读取该地名 我使用以下代码来读取查找当前位置 void mapView MKMapView mapView1 didUpdateUserL
  • 在 XCode 中链接静态 ObjC 库的过程

    我正在尝试链接到静态库 但不断收到链接器错误 我发现了一些发布示例的网站 但我无法看到我做错了什么 首先 我创建一个链接到我的库的项目 添加 gt 现有文件找到我的 xcodeproj 文件选择 将项目复制到目标组文件夹 选择我的宿主项目作
  • UIPanGestureRecognizer 对坐标的限制

    我在主 UIView 中添加了一个子视图 称为panel 并且我向其中添加了gestureRecognizer 因为我希望它只能在Y轴上拖动并且只能在某些限制下 即160 300 超过300它不能拖动 我以这种方式实现了手势处理 IBAct
  • 如何在 OSX 上安装 LaTeX .sty 文件?

    我设置了一个 LaTeX 项目 tex documents some file tex support todonotes sty where some file tex uses todonotes usepackage colorinl

随机推荐