【问题标题】:Keychain - Secure Data Storage钥匙串 - 安全数据存储
【发布时间】:2013-03-16 18:14:41
【问题描述】:

我正在开发一个带有钥匙串实现的应用程序。我能够创建并将数据保存到钥匙串中。我使用的是 Apple 提供的Keychain Wrapper classes

根据要求,我必须在 KeyChain 中实现尽可能好的安全性(安全团队指出了漏洞,例如它在越狱设备上的可访问性)。

有人能给我指路吗?

【问题讨论】:

    标签: ios objective-c xcode keychain


    【解决方案1】:

    我还使用您引用的相同 Wrapper 在应用程序中实现了钥匙串 long Back ,但是,当然有很多修改。

    Keychain 基本上是相当安全的。根据 Apple 的说法,它是一个加密容器,为多个应用程序保存安全信息,这意味着当 keychain 被锁定时,任何人都无法访问其受保护的内容。

    在 iOS 中,只有创建钥匙串的应用程序才能访问它。 根据Apple的文档,iOS可以选择Memory-Cache或者Disk Cache。

    但从 iOS 4.xx++ 开始,它只是磁盘缓存(不知道为什么),因此总是创建一个 sqlite DB ,其中钥匙串中的所有数据都存储对应于特定的标识符。

    Sqlite DB 可以在 root 或越狱设备上被黑客入侵。

    保护钥匙串

    1 在添加或添加时添加安全关键字“kSecAttrAccessibleWhenUnlockedThisDeviceOnly
    在方法“SecItemUpdate”和“SecItemAdd”上更新钥匙串中的数据。

    类似:-

    - (void)writeToKeychain
    {
        NSDictionary *attributes = NULL;
        NSMutableDictionary *updateItem = NULL;
        OSStatus result;
    
        if (SecItemCopyMatching((CFDictionaryRef)genericPasswordQuery, (CFTypeRef *)&attributes) == noErr)
        {
            updateItem = [NSMutableDictionary dictionaryWithDictionary:attributes];
    
            [updateItem setObject:[genericPasswordQuery objectForKey:(id)kSecClass] forKey:(id)kSecClass];
    
            NSMutableDictionary *tempCheck = [self dictionaryToSecItemFormat:keychainItemData];
            [tempCheck removeObjectForKey:(id)kSecClass];
    
    #if TARGET_IPHONE_SIMULATOR
            [tempCheck removeObjectForKey:(id)kSecAttrAccessGroup];
    #endif
    
            [updateItem setObject:(id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible];
            result = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck);
            NSAssert( result == noErr, @"Couldn't update the Keychain Item." );
            CFRelease(attributes);
        }
        else
        {
            [keychainItemData setObject:(id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible];
            result = SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
            NSAssert( result == noErr, @"Couldn't add the Keychain Item." );
        }
    }
    

    2 在添加到钥匙串之前加密数据。我使用了 AES-128 加密。 还要确保用于加密的密钥是 RSA 密钥。(由 SSL Web 服务发送)。

    注意:-钥匙串数据存储在 iPhone 上的/private/var/Keychains/keychain-2.db 文件中。

    希望对你有帮助。

    【讨论】:

    • 非常感谢。这解释了我在尝试将 kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(id)kSecAttrAccessible 设置为钥匙串包装器中的“setObject”时遇到的错误。
    【解决方案2】:
        [attributeDict setObject:(__bridge id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-03
      • 2021-10-01
      • 2014-08-09
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      相关资源
      最近更新 更多