从“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”中提取“用户名”? 的相关文章

  • WiFi 网络变化是否有 NSNotificationCenter 通知?

    我想在我的 Cocoa 应用程序中订阅 WiFi 网络更改 但我无法找到合适的事件来订阅 WiFi 网络变化是否有 NSNotificationCenter 通知 据我所知 我会用CoreWLAN https developer apple
  • 受信任的 1.5 小程序可以执行系统命令吗?

    如果是的话 这个能力有什么限制吗 具体来说 我需要以 Mac OSX 为目标 我以前用过这个在 Windows 系统上启动东西 但从未在 Mac 上尝试过 public void launchScript String args Strin
  • 如何在 swiftUI (macOS) 中检测按键按下和释放

    除了标题之外没什么可说的 我希望能够在按下按键和释放按键时 在 macOS 上 在 swiftUI 视图中执行操作 在 swiftUI 中是否有任何好的方法可以做到这一点 如果没有 有什么解决方法吗 不幸的是 键盘事件处理是其中一个令人痛苦
  • 为什么 Objective-C 方法名称的最后一部分必须带有参数(当有多个部分时)?

    在 Objective C 中 您不能声明最后一个组件不带参数的方法名称 例如 以下内容是非法的 void take id theMoney andRun void take id yourMedicine andDontComplain
  • dyld:惰性符号绑定失败

    当我尝试运行时遇到一个奇怪的错误gatsby develop在新创建的 gatsby 项目中 这不应该与 gatsby js 静态站点生成器 有任何关系 因为我在不同的场合也遇到了相同的错误 当我跑步时gatsby develop在我的一个
  • 在 XCode 7.0.1 中设置 VTK 6.1

    所以 我遇到了问题VTK 可视化工具包 http www vtk org在 Mac OSX 上工作 特别是让它在XCode https developer apple com xcode 我让它工作并将在下面发布这个问题的答案 截至 201
  • AVCaptureDevice 找不到任何设备

    这行代码是我今天遇到的问题 macOS 应用程序 NSArray devices AVCaptureDevice devicesWithMediaType AVMediaTypeVideo 我更新Xcode后 系统总是让我空着devices
  • .profile 无法从 Mac 终端运行

    我有一个 profile 文件 我正在终端中读取并使用别名 但在某些时候 别名由于没有明确的原因而停止工作 其他命令仍在工作 为了快速修复 我删除了 rm 并在用户目录中重新创建了 profile 文件 我可以看到 至少在该目录中没有 ba
  • 如何在可编写脚本的应用程序中将任意 AppleScript 记录传递给 Cocoa?

    我有一个 Cocoa 应用程序 其中包含 sdef XML 文件中描述的 AppleScript 字典 sdef 中定义的所有 AppleScript 类 命令等都是工作属性 除了我的 提交表单 命令 提交表单 命令是我尝试将任意信息哈希表
  • Objective-C:int值无故改变

    Objective C 我需要帮助保留 int 的值 无需我的命令 它就在我身上发生变化 最初的问题是 如何声明和保留 int 这在另一篇文章中得到了满足 Objective C 如何声明和保留 int https stackoverflo
  • ios 在后台处理推送通知

    我想保存应用程序处于后台状态时到达的推送通知 我知道关于 void application UIApplication application didReceiveRemoteNotification NSDictionary userIn
  • iOS 视图控制器内存在被关闭后未释放

    当用户单击按钮时 它会显示一个带有两个视图控制器的新选项卡栏视图控制器 我是这样做的 ACLevelDownloadController dvc ACLevelDownloadController alloc initWithNibName
  • Objective-c 中的块递归

    当执行涉及 Objective C 块的递归时 我在 iOS 应用程序中收到 EXC BAD ACCESS 信号 这是简化的代码 void problematicMethod FriendInfo friendInfo onComplete
  • 自定义 UITableViewCell 选择样式?

    当我点击我的UITableViewCell 当我单击单元格时 背景部分 我的背景图像未覆盖的区域 会变成蓝色 另外 所有的UILabel单击时单元格上的 s 变为白色 这就是我想要的 然而 我不想要的是当我点击它时的蓝色背景 但如果我这样做
  • 错误域=AVFoundationErrorDomain代码=-11814“无法记录”

    它不断给我错误 错误域 AVFoundationErrorDomain代码 11814 无法记录 我不确定问题是什么 我试图在拍照后计数器达到 1 时录制声音 static int counter counter will always b
  • 如何在 macOS 上将 Git 升级到最新版本?

    我刚刚购买了一台装有 OS X Lion 的新 Mac 我在终端中检查了默认安装的 git 版本 我得到了答案 git version gt git version 1 7 5 4 我想将 git 升级到最新版本 1 7 8 3 因此我下载
  • iOS 中的 CSV 逐行解析

    我正在 Objective c 中解析 CSV 文件 该文件包含如下内容 line 40 Rising searches line 41 nabi avc Breakout line 42 stonewall 700 line 43 med
  • iphone:如何停止快门动画?

    我有两个问题 1 我想知道如何在相机加载时停止快门动画 我正在使用 UIImagePickerController 我已经参考了堆栈溢出的许多答案 但没有成功 2 我在相机中有一个自定义按钮 使用cameraOverlayView并想通过单
  • 拖动时获取MKAnnotation的坐标

    我正在根据用户添加的注释的位置创建一条路径 MKPolyline 我想允许用户通过拖动引脚来更改路径 我目前可以做到这一点 但 MKPolyline 不会更新 直到引脚被放下 我实施了 void mapView MKMapView mapV
  • OSX bash 最小化窗口

    在 Mac 中并使用 bash shell 我想执行一个包含单个命令 启动 Jupyter Lab 的文件并立即最小化终端窗口 有没有办法在不安装第三方软件的情况下做到这一点 是的 只需使用osascript https ss64 com

随机推荐