4

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

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

有人可以给我方向吗?

4

2 回答 2

7

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

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

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

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

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

保护钥匙串

1 添加安全关键字“ ”,同时在方法“ ”&“ ”上kSecAttrAccessibleWhenUnlockedThisDeviceOnly添加或
更新钥匙串中的数据。SecItemUpdateSecItemAdd

就像是 :-

- (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 服务发送)。

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

希望它可以帮助你。

于 2013-03-16T18:19:21.317 回答
0
    [attributeDict setObject:(__bridge id)kSecAttrAccessibleWhenUnlockedThisDeviceOnly forKey:(__bridge id)kSecAttrAccessible];
于 2014-12-15T09:56:33.020 回答