Apple 钥匙串存储客户端身份,因此只有我的应用程序可以访问它

2024-01-21

Aim

我需要以安全的方式在 OS X 应用程序上存储客户端身份,以便只有我的应用程序可以访问它。没有提示请求权限。

Problem

当我尝试存储客户身份时,问题立即出现。 这是代码示例(到目前为止我已经绑定了什么):

- (BOOL)saveClientIdentity:(SecIdentityRef)clientIdentity error:(NSError**) error
{
    NSDictionary *attributes = @{
        (__bridge id)kSecAttrAccessible:(__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly,
        (__bridge id)kSecValueRef:(__bridge id)clientIdentity,
        (__bridge id)kSecAttrApplicationTag:[kMyKeychainAttrApplicationTag dataUsingEncoding: NSUTF8StringEncoding],
        (__bridge id)kSecAttrAccessGroup:kMyKeychainAttrAccessGroup
    };

    OSStatus status = SecItemAdd((__bridge CFDictionaryRef)attributes, NULL);
    // status == -25299
    …
}

我不断收到代码 -25299 并使用工具解释问题:

$ security error -25299
Error: 0xFFFF9D2D -25299 The specified item already exists in the keychain.

因此,它尝试覆盖全局客户端身份(我从未成功地为此应用程序编写客户端身份,因此不应该存在此类冲突),但我不知道该怎么做。 它必须仅对于此应用程序是私有的。

我验证了相应加载代码会发生什么。它加载了我的开发者身份,但我不希望这样。

- (SecIdentityRef)clientIdentity
{
    NSDictionary *attributes =
    @{
      (__bridge id)kSecClass:(__bridge id)kSecClassIdentity,
      (__bridge id)kSecAttrAccessible:(__bridge id)kSecAttrAccessibleAlwaysThisDeviceOnly,
      (__bridge id)kSecAttrApplicationTag:[kMyKeychainAttrApplicationTag dataUsingEncoding: NSUTF8StringEncoding],
      (__bridge id)kSecAttrAccessGroup:kMyKeychainAttrAccessGroup
      };

    CFTypeRef universalResult = NULL;
    OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)attributes, &universalResult);
    SecIdentityRef result = (SecIdentityRef)universalResult;
    if (result)
    {
        CFAutorelease(result);
    }
    if (status != noErr)
    {
        NSLog(@"Failed to load client identity: %@", NSErrorFromStatusErrorCode(status));
    }
    return result;
}

Notes

我需要对 iOS 使用相同的代码,但是这里应该没有问题,因为默认情况下 iOS 钥匙串不在应用程序之间共享。


我找到了很好的解决方案。 诀窍是创建自定义钥匙链并将客户身份存储在该钥匙链中。

所以基本上有树步骤。

  1. 首先创建或打开自定义钥匙串:

    NSString *keychainpath  = self.customKeychainPath;
    unsigned char password[SHA_DIGEST_LENGTH];
    GenerateCustomKeychainPassword(password);
    OSStatus status = SecKeychainCreate(keychainpath.UTF8String,
                                        SHA_DIGEST_LENGTH,
                                        password,
                                        NO,
                                        NULL,
                                        &customKeychain);
    
    if (status == errSecDuplicateKeychain)
    {
        status = SecKeychainOpen(keychainpath.UTF8String, &customKeychain);
        if (status == errSecSuccess)
        {
            status = SecKeychainUnlock(customKeychain,
                                       SHA_DIGEST_LENGTH,
                                       password,
                                       TRUE);
    
            if (status != errSecSuccess)
            {
                NSLog(@"%s Failed to unlock custom keychain: %@",
                           __PRETTY_FUNCTION__, NSErrorFromStatusErrorCode(status));
            }
        }
    }
    else if (status != errSecSuccess)
    {
        NSLog(@"%s Failed to unlock custom keychain: %@",
                   __PRETTY_FUNCTION__, NSErrorFromStatusErrorCode(status));
    }
    
  2. 然后将客户身份添加到该钥匙串中

    OSStatus status = errSecSuccess;
    
    CFTypeRef  persistent_ref = NULL;
    NSDictionary *dict = @{
                           (id)kSecValueRef:(id)secItem,
                           (id)kSecReturnPersistentRef:(id)kCFBooleanTrue,
    #if !TARGET_OS_IPHONE
                           (id)kSecUseKeychain:(__bridge id)customKeychain,
    #endif
                           };
    status = SecItemAdd((CFDictionaryRef)dict, &persistent_ref);
    NSCAssert(status != errSecParam, @"Wrong contents of dictionary");
    if (status == errSecDuplicateItem)
    {
        NSLog(@"%s Item: %@ already exists", __PRETTY_FUNCTION__, secItem);
        return NULL;
    }
    return (CFDataRef)persistent_ref;
    
  3. 并从钥匙串中读取项目(persistent_ref可以存储在用户默认值中)

    NSDictionary *dict = @{
                           (id)kSecClass:(__bridge id)itemType,//kSecClassIdentity,
                           (id)kSecReturnRef:(id)kCFBooleanTrue,
                           (id)kSecValuePersistentRef:persistantRef,
    #if !TARGET_OS_IPHONE
                           (id)kSecUseKeychain:(__bridge id)customKeychain,
    #endif
                           };
    
    OSStatus status =  SecItemCopyMatching((CFDictionaryRef)dict, &result);
    NSCAssert(status != errSecParam, @"Invalid arguments");
    
    return result;
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Apple 钥匙串存储客户端身份,因此只有我的应用程序可以访问它 的相关文章

  • Mac OS X 下的 JavaFX:系统菜单和模态窗口

    我目前面临 Mac OS X 下的 JavaFX 菜单和模式对话框的问题 我正在使用该方法 MenuBar setUseSystemMenuBar true 为了将系统菜单栏用于我的应用程序菜单 这工作正常 但如果模式对话框打开 菜单不会被
  • 将第 3 方库 ZXing 导入 Xcode

    我尝试了多种方法将第 3 方库 ZXing 导入我的 iOS 应用程序 但所有方法都很痛苦 或者根本不起作用 如果有人可以建议我做错了什么 或者提出导入 ZXing 等库的更好方法 我将非常感激 一定比这个容易 这就是我所做的 结果是 My
  • 最小的 iOS 蓝牙管理器示例

    我一直在构建一个最小的示例 用于使用 iOS 5 0 中的 BluetoothManager 私有框架来检测附近的蓝牙设备 使用此问题中找到的答案 寻找触手可及的通用蓝牙设备 https stackoverflow com question
  • 在 Objective-C 中比较 2 个字符串

    我写了以下代码 if depSelectedIndice gt 1 comSelectedIndice gt 1 NSLog depart elemet d depSelectedIndice NSLog depart elemet d c
  • NSString – 静态还是内联?有性能提升吗?

    如果我写的话会有任何性能提升吗 NSString helloStringWithName NSString name static NSString formatString Hello return NSString stringWith
  • 一个好的8086模拟器[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 mac 有没有 8086 模拟器 如果是 哪个是最好的 除非您需要真正的原始 8086 带有自定义操作系统 否则您应该尝试 DosBox
  • 如何知道我的应用程序使用了多少 iCloud 空间?

    有没有办法查看我的应用程序正在备份到 iCloud 的内容以及它消耗了多少内存 Settings gt iCloud gt Storage Backup gt Manage Storage将显示正在备份的总计内容 iOS 会备份位于应用程序
  • 如何将 UILabel 的值绑定到实例变量?

    我是 mac objective c 的新手 我的问题是 我想知道是否可以将 UILabel 文本绑定到变量 而不必在值更改时手动设置文本 例如 在 Mac OS 上 当我打开新的 Finder 窗口并删除文件时 任务栏中的全局可用空间就会
  • 减少 CoreData 的调试输出?

    我正在开发一个使用 CoreData 的 iOS macOS 项目 它工作正常 但它会向控制台输出大量调试信息 这使得控制台无法使用 因为我的打印语句隐藏在所有与 CoreData 相关的内容中 我有一个非常简单的 CoreData 设置
  • ios水平居中约束问题?

    I am having hard time in learning constraints auto layout in iOS I have used any width any height I have a storyboard sc
  • Mac OS X 上的 .dll 等效项 [重复]

    这个问题在这里已经有答案了 我来自一个Windows背景 我习惯于通过创建 dll 然后分发所有库和文档 因此 如果用户想要使用它 他会添加对库的引用并使用它 但是 在 Mac 中 我正在开发 SDK 并且我想要一种方法creating a
  • 从 robovm 项目创建 iOS 静态库(JNI 中的 BAD_ACCESS)

    我有大量的Java代码 只有计算函数 没有UI 我想在iOS中将其重用为静态库 我的方法是使用robovm http www robovm com并按照robovm论坛中两篇文章中描述的非官方方式创建静态库 1 基本方式 https gro
  • gem install mysql:无法构建 gem 本机扩展 (Mac Lion)

    我为 Mac OS X Lion 安装了 MySQL 5 5 27 来自 dmg 现在我尝试安装 mysql gem gem install mysql Building native extensions This could take
  • 在 iOS 应用程序中拨打电话

    我有一些代码尝试在应用程序中进行调用 但它似乎不起作用 UIApplication myApp UIApplication sharedApplication NSString theCall NSString stringWithForm
  • 如何删除 UITableView 中的缩进?

    首先 我对此很陌生 我很可能忘记了一些非常简单的事情 问题 我正在制作一个应用程序 在 a 中显示来自 imgur com 的随机图像tableView 由于某种原因 所有单元格都会缩进少量 如下图所示 我摆弄了许多设置storyboard
  • 自定义代码访问安全属性

    我创建了以下属性 Serializable AttributeUsage AttributeTargets Class AttributeTargets Method AllowMultiple true Inherited true pu
  • 使用 UITabBarController 时覆盖整个屏幕的视图?

    我想在 UITabBarController 设置中在整个屏幕上覆盖 HUD 样式的透明图形 执行此操作的按钮位于第一个选项卡的屏幕 FirstViewController 中 并且覆盖层也应该覆盖选项卡 这可能吗 您可以将新视图直接附加到
  • iOS7 由 Interface Builder 生成的 NSIBPrototypingLayoutConstraint 自动布局约束问题

    以前在 iOS6 中 我的聊天消息视图控制器使用 IB 中精心构建的自动布局约束正确显示和动画 下面是 Xcode 5 中删除了约束的当前视图层次结构 升级到 XCode 5 和 iOS7 后 我发现我需要清除所有旧的约束才能解决默认的半透
  • .NET 中是否有内置函数可以对密码进行哈希处理?

    我看到这个问题加密 散列数据库中的纯文本密码 https stackoverflow com questions 287517 encrypting hashing plain text passwords in database 我知道我
  • 无法将项目添加到 NSMutableArray ivar

    我的目标是将字符串添加到数组中 并在我调用的方法中执行此操作 在这个方法中 我在数组中得到一个空值 但不知道为什么 我在课程开始时有这样的内容 NSMutableArray listOfEvents 以及我在每个事件上调用的方法 void

随机推荐