我正在开发一个带有钥匙串(keychain)实现的应用程序。我能够创建数据并将其保存到钥匙串(keychain)中。我正在使用 Keychain Wrapper classes由 Apple 提供。
根据要求,我必须在 KeyChain 中实现尽可能好的安全性(安全团队指出了缺陷,例如它在越狱设备上的可访问性)。
有人能给我指路吗?
最佳答案
我还使用您引用的相同 Wrapper 在应用程序 long Back 中实现了钥匙串(keychain),但是,当然有很多修改。
Keychain 基本上是非常安全的。根据 Apple 的说法,它是一个加密的容器,可以保存多个应用程序的安全信息,这意味着当钥匙串(keychain)被锁定时,没有人可以访问其 protected 内容。
在 iOS 中,只有创建钥匙串(keychain)的应用程序才能访问它。 根据 Apple 的文档,iOS 可以选择 Memory-Cache 或 Disk Cache。
但是从 iOS 4.xx++ 开始,它只是磁盘缓存(不知道为什么),因此总是创建一个 sqlite DB ,钥匙串(keychain)中的所有数据都存储在对应于特定标识符的位置。
Sqlite 数据库可以在 root 或越狱设备上被黑客攻击。
保护钥匙串(keychain)
1 在添加或
时添加安全关键字“kSecAttrAccessibleWhenUnlockedThisDeviceOnly
”
在方法“SecItemUpdate
”和“SecItemAdd
”上更新钥匙串(keychain)中的数据。
类似的东西:-
- (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 在添加到钥匙串(keychain)之前加密数据。我使用了 AES-128 加密。 还要确保用于加密的 key 是 RSA key 。(由 SSL Web 服务发送)。
注意:-钥匙串(keychain)数据存储在 iPhone 上的 /private/var/Keychains/keychain-2.db
文件中。
希望对你有帮助。
关于ios - 钥匙串(keychain) - 安全数据存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15452847/